关于string的sizeof问题
这里有个string数组,
string iceCream[] = {
"pralines & cream",
"fudge ripple",
"jamocha almond fudge",
"wild mountain blackberry",
"raspberry sorbet",
"lemon swirl",
"rocky road",
"deep chocolate fudge"
};
我用sizeof(*iceCream)求值,在我机器上是32,不知道为何原因,那位高手给我解释一下?
我用的vc2005。
[解决办法]
sf,帮顶
[解决办法]
string是系统内部类,它的大小是32
cout << sizeof(string) << endl;
输出32
[解决办法]
你试一下
cout << sizeof(string) << endl;
cout << sizeof(iceCream) << endl;
cout << sizeof(*iceCream) << endl;
就明白了
[解决办法]
因机器不同会有所不同,
我的16
[解决办法]
string内部的实现是保存一个指针.
sizeof(string) 等同于 sizeof(char*), 一般的结果也就是4.
cout < < sizeof(string) < < endl; //结果是4
cout < < sizeof(iceCream) < < endl; //是(数组大小*sizeof(string)), 也就是8 * 4 = 32.
cout < < sizeof(*iceCream) < < endl; //*iceCream指向的是第1个string元素, 也就是sizeof(string), 结果是4
楼主可以把string iceCream[] = {
"pralines & cream",
"fudge ripple",
"jamocha almond fudge",
"wild mountain blackberry",
"raspberry sorbet",
"lemon swirl",
"rocky road",
"deep chocolate fudge" ,
"test abc" //增加一个元素测试下看看结果
};
[解决办法]
这是和编译器相关的!
[解决办法]
[解决办法]
[解决办法]
[解决办法]
你试一下
cout < < sizeof(string) < < endl;
cout < < sizeof(iceCream) < < endl;
cout < < sizeof(*iceCream) < < endl;
因为这个是随着着系统的不同而不同
[解决办法]
存指针
可以试下
char *str = new char[50];
sizeof(str)等于多少
字符串用指针表示,而指针占用的空间和字符串内容没有关系
...to 10楼
string不就是个类么= =bb
[解决办法]
对了,另外...一般不会用sizeof(string)的吧,我想|||
而且求string长度也不会用strlen的吧
strlen是针对C字符串的....
[解决办法]
[解决办法]
字符串的长度要用strlen来求。
