[英]STL basic_string length with null characters
Why is it that you can insert a '\\0' char in a std::basic_string and the .length() method is unaffected but if you call char_traits<char>::length(str.c_str())
you get the length of the string up until the first '\\0' character? 为什么你可以在std :: basic_string中插入一个'\\ 0'char并且.length()方法不受影响但是如果你调用char_traits<char>::length(str.c_str())
你得到的长度直到第一个'\\ 0'字符的字符串?
eg 例如
string str("abcdefgh");
cout << str.length(); // 8
str[4] = '\0';
cout << str.length(); // 8
cout << char_traits<char>::length(str.c_str()); // 4
Great question! 好问题!
The reason is that a C-style string is defined as a sequence of bytes that ends with a null byte. 原因是C样式字符串被定义为以空字节结尾的字节序列。 When you use .c_str()
to get a C-style string out of a C++ std::string
, then you're getting back the sequence the C++ string stores with a null byte after it. 当您使用.c_str()
从C ++ std::string
获取C样式的字符串时,您将获得C ++字符串存储的序列,其后面带有空字节。 When you pass this into strlen
, it will scan across the bytes until it hits a null byte, then report how many characters it found before that. 当您将其传递给strlen
,它将扫描字节,直到它达到空字节,然后报告它之前找到的字符数。 If the string
contains a null byte, then strlen
will report a value that's smaller than the whole length of the string, since it will stop before hitting the real end of the string. 如果string
包含空字节,则strlen
将报告一个小于字符串整个长度的值,因为它将在命中字符串的实际结束之前停止。
An important detail is that strlen
and char_traits<char>::length
are NOT the same function. 一个重要的细节是strlen
和char_traits<char>::length
不是同一个函数。 However, the C++ ISO spec for char_traits<charT>::length
(§21.1.1) says that char_traits<charT>::length(s)
returns the smallest i
such that char_traits<charT>::eq(s[i], charT())
is true. 但是, char_traits<charT>::length
(§21.1.1)的C ++ ISO规范说char_traits<charT>::length(s)
返回最小的 i
,使得char_traits<charT>::eq(s[i], charT())
是真的。 For char_traits<char>
, the eq
function just returns if the two characters are equal by doing a ==
comparison, and constructing a character by writing char()
produces a null byte, and so this is equal to saying "where is the first null byte in the string?" 对于char_traits<char>
,如果通过执行==
比较来使两个字符相等,则eq
函数返回,并且通过写入char()
构造字符会产生空字节,因此这等于说“哪里是第一个”字符串中的空字节?“ It's essentially how strlen
works, though the two are technically different functions. 它本质上是strlen
工作方式,尽管两者在技术上是不同的功能。
A C++ std::string
, however, it a more general notion of "an arbitrary sequence of characters." 但是,C ++ std::string
是一个更为笼统的“任意字符序列”的概念。 The particulars of its implementation are hidden from the outside world, though it's probably represented either by a start and stop pointer or by a pointer and a length. 其实现的细节隐藏在外部世界,尽管它可能由开始和停止指针或指针和长度表示。 Because this representation does not depend on what characters are being stored, asking the std::string
for its length tells you how many characters are there, regardless of what those characters actually are. 因为这种表示不依赖于存储的字符,所以询问std::string
的长度可以告诉你有多少个字符,无论这些字符实际是什么。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.