在c++中,string是定长的。
根据实现的不同string类型可能只存储指针,可能存储部分数据、剩余部分用指针指向。
在VC++6.0中,string占用16个字节。
string s1="hello";
string s2="helloahelloahelloahelloahelloahello";
struct M
{
int x;
string s;
};
cout<<sizeof(string)<<endl;
cout<<sizeof(s1)<<endl;
cout<<sizeof(s2)<<endl;
cout<<sizeof(M)<<endl;
输出:16,16,16,20。