C++ std::string 是否包含空终止符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11752705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:30:16  来源:igfitidea点击:

Does std::string contain null terminator?

c++stringnull

提问by mister

Will the below string contain the null terminator '\0' ?

下面的字符串会包含空终止符 '\0' 吗?

std::string temp = "hello whats up";

Thanks! :)

谢谢!:)

回答by jahhaj

No, but if you say temp.c_str()a null terminator will be included in the return from this method.

不,但是如果您说temp.c_str()空终止符将包含在此方法的返回中。

It's also worth saying that you can include a null character in a string just like any other character.

还值得一提的是,您可以像任何其他字符一样在字符串中包含空字符。

string s("hello");
cout << s.size() << ' ';
s[1] = '
std::string temp = "hello whats up";
'; cout << s.size() << '\n';

prints

印刷

5 5

5 5

and not 5 1as you might expect if null characters had a special meaning for strings.

5 1如果空字符对字符串具有特殊含义,则不会像您所期望的那样。

回答by mister

Not in C++03, and it's not even guaranteed before C++11 that in a C++ std::string is continuous in memory. Only C strings (char arrays which are intended for storing strings) had the null terminator.

不在 C++03 中,在 C++11 之前甚至不能保证 C++ std::string 在内存中是连续的。只有 C 字符串(用于存储字符串的字符数组)具有空终止符。

In C++11 and later, mystring.c_str()is equivalent to mystring.data()is equivalent to &mystring[0], and mystring[mystring.size()]is guaranteed to be '\0'.

在 C++11 及更高版本中,mystring.c_str()等效于mystring.data()等效于&mystring[0],并且mystring[mystring.size()]保证为'\0'

回答by aniliitb10

This depends on your definition of 'contain' here. In

这取决于您在此处对“包含”的定义。在

std::string s1 = "ab##代码####代码##cd";   // s1 contains "ab",       using string literal
std::string s2{"ab##代码####代码##cd", 6}; // s2 contains "ab##代码####代码##cd", using different ctr
std::string s3 = "ab##代码####代码##cd"s;  // s3 contains "ab##代码####代码##cd", using ""s operator

there are few things to note:

有几点需要注意:

  • temp.size()will return the number of characters from first hto last p(both inclusive)
  • But at the same time temp.c_str()or temp.data()will return with a nullterminator
  • Or in other words int(temp[temp.size()])will be zero
  • temp.size()将先返回字符数h去年p(均包括在内)
  • 但同时temp.c_str()还是temp.data()会带着null终结符返回
  • 或者换句话说int(temp[temp.size()])为零

I know, I sound similar to some of the answers here but I want to point out that sizeof std::stringin C++is maintained separately and it is not likein Cwhere you keep counting unless you find the first nullterminator.

我知道,我的声音有些类似于此的答案,但我想指出的是,sizestd::stringC++单独保存,它不是像C哪里你保持,除非你找到的第一个计数null终止。

To add, the story would be a little different if your string literalcontains embedded \0. In this case, the construction of std::stringstops at first nullcharacter, as following:

另外,如果您string literal包含嵌入的\0. 在这种情况下,构造std::string在第一个null字符处停止,如下所示:

##代码##

References:

参考:

回答by Nawaz

Yes if you call temp.c_str(), then it will return null-terminated c-string.

是的,如果您调用temp.c_str(),那么它将返回以空字符结尾的 c 字符串。

However, the actual data stored in the object tempmay not be null-terminated, but it doesn't matter and shouldn't matter to the programmer, because when then programmer wants const char*, he would call c_str()on the object, which is guaranteed to return null-terminated string.

但是,对象中存储的实际数据temp可能不是以null结尾的,但这对程序员来说无关紧要,因为当程序员想要时const char*,他会调用c_str()该对象,该对象保证返回null - 终止的字符串。

回答by Naps62

With C++ strings you don't have to worry about that, and it's possibly dependent of the implementation.

使用 C++ 字符串,您不必担心这一点,它可能取决于实现。

Using temp.c_str()you get a C representation of the string, which will definitely contain the \0char. Other than that, i don't really see how it would be useful on a C++ string

使用temp.c_str()您可以获得字符串的 C 表示,其中肯定会包含\0字符。除此之外,我真的不知道它在 C++ 字符串上有什么用

回答by Superman

std::stringinternally keeps a count of the number of characters. Internally it works using this count. Like others have said, when you need the string for display or whatever reason, you can its c_str()method which will give you the string with the null terminator at the end.

std::string内部保持字符数的计数。它在内部使用此计数工作。就像其他人所说的那样,当您需要显示字符串或出于任何原因时,您可以使用它的c_str()方法,该方法将为您提供末尾带有空终止符的字符串。