理解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
<think>嗯,用户要求翻译一段C++编译输出日志。看起来是编译一个名为mycpp的Python扩展模块时的警告和构建信息。 用户可能是开发人员,正在处理一个涉及Eigen库和Pybind11的项目(从路径名foundationpose-main能看出)。ta在编译时遇到了两个格式字符串警告,最后成功生成了so文件。用户没提问具体问题,只是要求翻译,说明ta更关注内容理解而非技术细节。 翻译时要注意: 1 保留技术术语原意(如"warning"译成"警告") 2 路径保持原格式(/mnt/e/...) 3 编译器信息准确转换(如"format '%d' expects...") 4 构建进度信息([100%])保留 5 LTO相关提示属于编译器优化细节,直译即可 特别要注意两个警告的核心问题:printf的%d格式符用于size_t类型(在64位系统是long unsigned int),应该用%ld。中文表述时要明确类型不匹配的本质。 最后成功信息可以简洁处理,"Built target"译为"已构建目标"更符合中文习惯。用户没要求解释技术问题,所以翻译保持简洁,但最后的"相关问题"可以引导技术讨论。</think>以下是编译输出的中文翻译: ``` /mnt/e/wsl/foundationpose-main/mycpp/src/app/pybind_api.cpp: 在函数‘vectorMatrix4f cluster_poses(float, float, const vectorMatrix4f&, const vectorMatrix4f&)’中: /mnt/e/wsl/foundationpose-main/mycpp/src/app/pybind_api.cpp:26:38: 警告:格式‘%d’需要类型为‘int’的参数,但第2个参数的类型为‘std::vector<Eigen::Matrix<float, 4, 4>, Eigen::aligned_allocator<Eigen::Matrix<float, 4, 4> > >::size_type’ {即 ‘long unsigned int’} [-Wformat=] 26 | printf("原始候选姿态数量 = %d\n",poses_in.size()); | ~^ ~~~~~~~~~~~~~~~ | | | | int long unsigned int | %ld /mnt/e/wsl/foundationpose-main/mycpp/src/app/pybind_api.cpp:66:42: 警告:格式‘%d’需要类型为‘int’的参数,但第2个参数的类型为‘std::vector<Eigen::Matrix<float, 4, 4>, Eigen::aligned_allocator<Eigen::Matrix<float, 4, 4> > >::size_type’ {即 ‘long unsigned int’} [-Wformat=] 66 | printf("聚类后的姿态数量: %d\n",poses_out.size()); | ~^ ~~~~~~~~~~~~~~~~ | | | | int long unsigned int | %ld [100%] 正在链接 CXX 共享模块 mycpp.cpython-39-x86_64-linux-gnu.so lto-wrapper: 警告:使用2个LTRANS作业的串行编译 lto-wrapper: 注意:更多信息请参阅‘-flto’选项文档 make[2]: 离开目录 '/mnt/e/wsl/foundationpose-main/mycpp/build' [100%] 已构建目标 mycpp make[1]: 离开目录 '/mnt/e/wsl/foundationpose-main/mycpp/build' ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值