理解vector<T>::size_type类型

本文探讨了size_type和int的区别,强调size_type通常为无符号类型且长度与int相同,但考虑到兼容性,推荐在类实现中使用如vector&lt;int&gt;::size_type。这样做有助于避免未来因类型定义改变导致的兼容性问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

size_type是无符号类型,通常是unsigned,定义在类里是一种设计要求
补充:
int有符号,size_type无符号,但长度一般一样(注意是一般),基于类的实现封装为考虑,推荐使用vector<int>::size_type,这样可以提高代码的兼容性,否则如果有一天库的作者把size_type定义为:
typedef other_type size_type; // 只是个假设
原来的代码中如果int不兼容other_type,就要出问题了
/mnt/e/wsl/foundationpose-main/mycpp/src/app/pybind_api.cpp: In function ‘vectorMatrix4f cluster_poses(float, float, const vectorMatrix4f&, const vectorMatrix4f&)’: /mnt/e/wsl/foundationpose-main/mycpp/src/app/pybind_api.cpp:26:38: warning: format ‘%d’ expects argument of type ‘in ’, but argument 2 has type ‘std::vector<Eigen::Matrix<float, 4, 4>, Eigen::aligned_allocator<Eigen::Matrix<float, 4, 4> > >::size_type’ {aka ‘long unsigned int’} [-Wformat=] 26 | printf("num original candidates = %d\n",poses_in.size()); | ~^ ~~~~~~~~~~~~~~~ | | | | int std::vector<Eigen::Matrix<float, 4, 4>, Eigen::aligned_allocator<Eigen::Matrix<float, 4, 4> > >::size_type {aka long unsigned int} | %ld /mnt/e/wsl/foundationpose-main/mycpp/src/app/pybind_api.cpp:66:42: warning: format ‘%d’ expects argument of type ‘in ’, but argument 2 has type ‘std::vector<Eigen::Matrix<float, 4, 4>, Eigen::aligned_allocator<Eigen::Matrix<float, 4, 4> > >::size_type’ {aka ‘long unsigned int’} [-Wformat=] 66 | printf("num of pose after clustering: %d\n",poses_out.size()); | ~^ ~~~~~~~~~~~~~~~~ | | | | int std::vector<Eigen::Matrix<float, 4, 4>, Eigen::aligned_allocator<Eigen::Matrix<float, 4, 4> > >::size_type {aka long unsigned int} | %ld [100%] Linking CXX shared module mycpp.cpython-39-x86_64-linux-gnu.so lto-wrapper: warning: using serial compilation of 2 LTRANS jobs lto-wrapper: note: see the ‘-flto’ option documentation for more information make[2]: Leaving directory '/mnt/e/wsl/foundationpose-main/mycpp/build' [100%] Built target mycpp make[1]: Leaving directory '/mnt/e/wsl/foundationpose-main/mycpp/build' 翻译一下
最新发布
07-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值