Will the below string contain the null terminator '
Will the below string contain the null terminator '\0' ?
下面的字符串是否包含null终结者'\0' ?
std::string temp = "hello whats up";
Thanks! :)
谢谢!:)
6 个解决方案
#1
58
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] = '\0';
cout << s.size() << '\n';
prints
打印
5 5
5个5
and not 5 1 as you might expect if null characters had a special meaning for strings.
如果空字符对于字符串有特殊的含义,就不像你想象的那样是51。
#2
33
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字符串(用于存储字符串的字符数组)具有null终止符。
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中,mystr.c str()等价于mystring.data()等价于&mystring[0], mystring[mystr.size()]保证为'\0'。
#3
2
Yes if you call temp.c_str(), then it will return null-terminated c-string.
是的,如果您调用temp.c_str(),那么它将返回以null结尾的c-string。
However, the actual data stored in the object temp may 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结尾的字符串。
#4
2
This depends on your definition of 'contain' here. In
这取决于你对“包含”的定义。在
std::string temp = "hello whats up";
there are few things to note:
有几件事需要注意:
temp.size()will return the number of characters from firsthto lastp- size()将返回从第一个h到最后一个p的字符数
- But at the same time
temp.c_str()ortemp.data()will return with anullterminator - 但与此同时,temp.c_str()或temp.data()将返回一个空终止符
- Or in other words
int(temp[temp.size()])will be zero - 换句话说,int(temp .size())将为零。
I know, I sound similar to some of the answers here but I want to point out that size of std::string in C++ is maintained separately and it is not like in C where you keep counting unless you find the first null terminator.
我知道,我听起来和这里的一些答案相似,但我想指出的是,在c++中,std::string的大小是分开维护的,不像在C中,你一直计数,除非你找到第一个空终止符。
To add, the story would be a little different if your string literal contains embedded \0. In this case, the construction of std::string stops at first null character, as following:
若要添加,如果字符串文字包含嵌入的\0,则情况会有所不同。在这种情况下,std::string的构造在第一个空字符处停止,如下所示:
std::string s1 = "ab\0\0cd"; // s1 contains "ab", using string literal
std::string s2{"ab\0\0cd", 6}; // s2 contains "ab\0\0cd", using different ctr
std::string s3 = "ab\0\0cd"s; // s3 contains "ab\0\0cd", using ""s operator
References:
引用:
- https://akrzemi1.wordpress.com/2014/03/20/strings-length/
- https://akrzemi1.wordpress.com/2014/03/20/strings-length/
- https://en.cppreference.com/w/cpp/string/basic_string/basic_string
- https://en.cppreference.com/w/cpp/string/basic_string/basic_string
#5
1
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 \0 char. Other than that, i don't really see how it would be useful on a C++ string
使用temp.c_str(),可以得到字符串的C表示形式,它肯定包含\0 char。除此之外,我不知道它在c++字符串中有什么用
#6
1
std::string internally 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.
字符串内部保存字符数。在内部,它使用这个计数。就像其他人说的,当你需要字符串显示或其他原因时,你可以使用它的c_str()方法,它会给你一个字符串,在末尾有空终止符。
' ?Will the below string contain the null terminat