C++ 字符串矩阵的sizeof

C++ 字符串矩阵的sizeof,c++,arrays,matrix,sizeof,C++,Arrays,Matrix,Sizeof,这些评论很好地解释了这一切。帮忙 string aZOM[][2] = {{"MoraDoraKora", "PleaseWorkFFS"},{"This is a nother strang.", "Orly?"}}; cout << sizeof("MoraDoraKora") <<" \n"; //Obviously displayes the size of this string... cout << sizeof(aZOM[0][0]) &l

这些评论很好地解释了这一切。帮忙

   string aZOM[][2] = {{"MoraDoraKora", "PleaseWorkFFS"},{"This is a nother strang.", "Orly?"}};
cout << sizeof("MoraDoraKora") <<" \n";
//Obviously displayes the size of this string...
cout << sizeof(aZOM[0][0]) << " \n";
//here's the problem, it won't display the size of the actual string... erm, what?

string example = aZOM[0][0];
cout << example << " \n";
cout << aZOM[0][1] << " \n";
//Both functions display the string just fine, but the size of referencing the matrix is the hassle.
string aZOM[][2]={{{“MoraDoraKora”,“plesseworkffs”},{“这是另一个strang.”,“Orly?”};

cout
sizeof
提供传递给它的对象的大小(以字节为单位)。如果给它一个
std::string
,它会给你
std::string
对象本身的大小。现在,我的对象为实际字符动态分配存储并包含指向它们的指针,但这不是对象本身的一部分

要获取
std::string
的大小,请使用其成员函数:

cout << aZOM[0][1].size() << " \n";

cout
sizeof
返回类型的大小,而不是指向的数据的大小

字符串通常是指向char的指针,其中链中的最后一个char的值为0


如果需要字符串的实际大小,可以使用
aZOM[0][0].length();对于第一个,您得到了const char*的大小?这似乎是更好的解释。:)@Skeen由于数组不作为
sizeof
的操作数进行数组到指针的转换,因此您可以获得数组的大小。