一篇不错的讲解中英对照的版本
#include <iostream>
#include <typeinfo>
using namespace std;
size_t count_calls()
{
static size_t ctr = 0; //第一次经过对象定义语句的时候进行初始化, 直到程序销毁
return ++ctr;
}
int main()
{
for (size_t i = 0; i != 10; i++)
cout << count_calls() << endl;
size_t a;
//变量所占的字节数B, 修改不同的解决方案平台得到不同的结果32, 64
cout << "a变量的类型是 " << typeid(a).name() << " 大小为:" << sizeof(a) * 8 << "位"<< endl;
int b;
unsigned int c;
cout << "b变量的类型是 " << typeid(a).name() << " 大小为:" << sizeof(b) * 8 << "位" << endl;
cout << "c变量的类型是 " << typeid(b).name() << " 大小为:" << sizeof(c) * 8 << "位" << endl;
/*
a变量的类型是 unsigned __int64 大小为:64位
b变量的类型是 unsigned __int64 大小为:32位
c变量的类型是 int 大小为:32位
结果表明, int 和 unsigned int 表示占据的空间相同, 但是在表示正数比如长度时候的无符号整数
表达范围为int的两倍, 使用size_t的时候可以达到实际的最大值
使用size_t可以兼具可移植性和高效性。拥有size_t类型的形参的函数经常有向上向下计数或索引数组的局部变量,
size_t类型通常是这些变量不错的选择。
*/
return 0;
}