简单的程序,测试vector的大小[c++]:
#include#include using namespace std;struct Node{ int id; vector score;};void DisplayVector(vector &v){ cout<<"sizeof(v) = "< <
本文共 1002 字,大约阅读时间需要 3 分钟。
简单的程序,测试vector的大小[c++]:
#include#include using namespace std;struct Node{ int id; vector score;};void DisplayVector(vector &v){ cout<<"sizeof(v) = "< <
输出结果:
sizeof(vector<char>) = 16
sizeof(vector<int>) = 16sizeof(vector<short>) = 16sizeof(vector<double>) = 16sizeof(vector<long>) = 16sizeof(vector<float>) = 16sizeof(Node) = 20sizeof(Node) = 20sizeof(v) = 1610 20 25修改了下主函数:
int main()
{ DisplaySize();Node node;
cout<<"sizeof(Node)_1 = "<<sizeof(node)<<endl;node.id = 1;
for(int i = 1; i <= 1000; i++) { node.score.push_back(i); } cout<<"sizeof(Node)_2 = "<<sizeof(node)<<endl; // 不管插入多少数据,sizeof(vector<>)大小不变// DisplayVector(node.score);
return 0;
}发现“不管插入多少数据,sizeof(vector<>)大小不变,始终为16”!
转载于:https://www.cnblogs.com/joeblackzqq/archive/2011/02/14/1954228.html