C ++ sizeof(字符串) [英] c++ sizeof( string )
本文介绍了C ++ sizeof(字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <cstdlib>
#include <iostream>
int main(int argc, char *argv[])
{
cout << "size of String " << sizeof( string );
system("PAUSE");
return EXIT_SUCCESS;
}
输出:
字符串的大小= 4
size of String = 4
这是否意味着,由于 sizeof(char)= 1字节(0到255)
,字符串只能容纳 4
个字符?
Does that mean that, since sizeof(char) = 1 Byte (0 to 255)
, string can only hold 4
characters?
推荐答案
从您的示例中尚不清楚字符串"是什么.如果您有:
It isn't clear from your example what 'string' is. If you have:
#include <string>
using namespace std;
然后 string
是 std :: string
,而 sizeof(std :: string)
为您提供类实例的大小及其实例数据成员,而不是字符串的长度.为此,请使用:
then string
is std::string
, and sizeof(std::string)
gives you the size of the class instance and its data members, not the length of the string. To get that, use:
string s;
cout << s.size();
这篇关于C ++ sizeof(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文