c ++中字符串的大小是多少?

时间:2017-05-08 20:31:08

标签: c++ string

在c ++中,如果我定义了一个字符串string x ="call"; ,x的大小是多少? 是4个字节吗?或者我应该计算空字节,以便大小为5?

2 个答案:

答案 0 :(得分:3)

一个字符串有一个函数size(),你可以调用它来获取它的长度:

int size = x.size();

答案 1 :(得分:0)

您可以通过调用string.size()从here进行检查,获得不带null终止符的大小。该页面中的示例是:

// string::size
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  std::cout << "The size of str is " << str.size() << " bytes.\n";
  return 0;
}

,输出为:

  

str的大小是11个字节

&#34;测试字符串&#34;正好是11个字节,所以没有空终止符。