sizeof操作符的作用:返回一个对象或类型名的字节长度
一、sizeof(type name)
二、sizeof(object)
三、sizeof object
#include <iostream>
#include <cassert>
#include <vector>
#include <cstddef>
#include <string>
using namespace std;
int main()
{
/*应用在指针类型上的sizeof操作符返回的是包含该类型地址所需的内存长度。
应用在引用类型上的sizeof操作符返回的是包含被引用对象所需的内存长度。*/
size_t ia;
ia =sizeof(ia);
ia=sizeof ia;
ia=sizeof(int);
int *pi =new int[12];
cout<<"pi: "<<sizeof(pi)<<endl<<"*pi "<<sizeof(*pi)<<endl;
cout<<"---------------->\t"<<endl;
string str=("test");
cout<<"str : "<<sizeof(str)<<endl;
string str1=(" black ");
string str2=("With you l ride or die tonight !");
string *ps =&str1;
cout<<"str1: "<<sizeof(str1)<<endl
<<"str2: "<<sizeof(str2)<<endl
<<"ps: "<<sizeof(ps)<<endl
<<"*ps: "<<sizeof(*ps)
<<endl;
//当sizeof应用在char类型上,在所有的实现里返回的都是1
cout<<"short: \t"<<sizeof(short)<<endl;
cout<<"short: \t"<<sizeof(short*)<<endl;
cout<<"short& : "<<sizeof(short&)<<endl;
cout<<"short[3]: "<<sizeof(short[3])<<endl;
size_t char_size=sizeof(char);
cout<<char_size<<endl;
}