C++库研究笔记——几个注意(size_t)
2013-07-29 09:32
232 查看
总之,用size_t 保证可移植性,保证其可以表示出最大需要表示的长度。
size_t 在stddef.h 中无命名空间
在<cstddef>有std::
size_t 与ptrdiff_t 要联合使用
When
signed and
unsigned, then
converted to unsigned before the comparison is performed. This is part of what are called the usual arithmetic conversions.http://stackoverflow.com/questions/4311538/strange-behaviour-with-for-loop-and-size-t
(ptrdiff_t 在64位下有更高的效率)
结果:
20
sizeof(short):2
sizeof(float):4
sizeof(double):8
sizeof(long):8
sizeof(size_t):8
sizeof(ptrdiff_t):8
sizeof(ptrdiff_t):8
size_t 在stddef.h 中无命名空间
在<cstddef>有std::
size_t 与ptrdiff_t 要联合使用
When
iis
signed and
sizeis
unsigned, then
iis
converted to unsigned before the comparison is performed. This is part of what are called the usual arithmetic conversions.http://stackoverflow.com/questions/4311538/strange-behaviour-with-for-loop-and-size-t
unsigned int vs.
size_t:
When to use std::size_t?
Difference
between size_t and std::size_t
About size_t and ptrdiff_t
(ptrdiff_t 在64位下有更高的效率)#include <iostream> #include <stddef.h> template <typename T, int N> int size(T (&) ) { return N; } int main() { int a[20]; std::cout<<size(a)<<std::endl; std::cout<<"sizeof(short):"<<sizeof(short)<<std::endl; std::cout<<"sizeof(float):"<<sizeof(float)<<std::endl; std::cout<<"sizeof(double):"<<sizeof(double)<<std::endl; std::cout<<"sizeof(long):"<<sizeof(long)<<std::endl; std::cout<<"sizeof(size_t):"<<sizeof(size_t)<<std::endl; std::cout<<"sizeof(ptrdiff_t):"<<sizeof(ptrdiff_t)<<std::endl; // error when not using #include <stddef.h> std::cout<<"sizeof(ptrdiff_t):"<<sizeof(std::ptrdiff_t)<<std::endl; return 0; }
结果:
20
sizeof(short):2
sizeof(float):4
sizeof(double):8
sizeof(long):8
sizeof(size_t):8
sizeof(ptrdiff_t):8
sizeof(ptrdiff_t):8
相关文章推荐
- 【学习笔记】汇编:关于几个标志位的注意点
- 循环神经网络训练时几个参数注意点的笔记
- C++ 库研究笔记——通过inline避免hpp 的mutiple definition 错误
- C++ 库研究笔记——拷贝构造函数的一个错误范例
- iOS开发笔记(六):UIButton需要注意的几个地方
- C++库研究笔记——函数名的宏定义
- C++库研究笔记——内存池实现
- [javase学习笔记]-7.5 构造函数须要注意的几个细节
- C++ 库研究笔记——Linux下创建文件夹
- C++库研究笔记——赋值操作符operator=的正确重载方式(三个准则)
- C++库研究笔记——操作符重载实现类型转换&这样做的意义
- C++ 库研究笔记——通过inline避免hpp 的mutiple definition 异常
- C++ 库研究笔记——Linux下创建文件夹
- 【C/C++】C++库研究笔记——函数名的宏定义
- C++ 库研究笔记——通过inline避免hpp 的mutiple definition 错误
- 运用数据挖掘技术研究方剂配伍规律应注意的几个问题
- [javase学习笔记]-7.5 构造函数需要注意的几个细节
- C++库研究笔记——policy-base 实现
- java学习笔记---Java可变参数方法重载时要注意的几个问题
- NHibernate学习笔记(1) —— 使用NHibernate中应注意的几个小问题