STL basic_string长度为空字符 [英] STL basic_string length with null characters
问题描述
为什么可以在std :: basic_string中插入一个'\0'char,而且.length()方法不受影响,但是如果调用 char_traits< char> :: length str.c_str())
你得到字符串的长度,直到第一个'\0'字符为止。
>
string str(abcdefgh);
cout<< str.length(); // 8
str [4] ='\0';
cout<< str.length(); // 8
cout<< char_traits< char> :: length(str.c_str()); // 4
b
$ b
原因是C风格字符串被定义为以空字节结尾的字节序列。当使用 .c_str()
从C ++ std :: string
中获取C风格的字符串时,你得到的序列C ++字符串存储与空字节之后。当你将它传递给 strlen
时,它将扫描字节,直到它命中一个空字节,然后报告它之前找到了多少字符。如果 string
包含一个空字节,则 strlen
将报告小于字符串整个长度的值,因为它会在到达字符串的真实结尾之前停止。
一个重要的细节是 strlen
和 char_traits< char> :: length
不是相同的函数。但是, char_traits< charT> :: length
(&Sect; 21.1.1)的C ++ ISO规范表示 char_traits< charT> :: length s)
返回最小 i
,以使 char_traits< charT> :: eq [i],charT())
为true。对于 char_traits< char>
, eq
函数只是返回如果两个字符相等, c> == 比较,并通过写 char()
构造一个字符产生一个空字节,因此这等于说where is字符串中的第一个空字节?它实际上是如何 strlen
工作,虽然这两个是技术上不同的功能。
A C ++ std :: string
,但是,它是一个更一般的概念一个任意的字符序列。其实现的细节从外部世界隐藏,尽管它可能由开始和停止指针或由指针和长度表示。因为这个表示不依赖于什么字符被存储,要求 std :: string
它的长度告诉你有多少字符,无论这些字符实际上是什么。
希望这有助于!
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?
e.g.
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. 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. 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. 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.
An important detail is that strlen
and char_traits<char>::length
are NOT the same function. 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. 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?" It's essentially how strlen
works, though the two are technically different functions.
A C++ std::string
, however, it a more general notion of "an arbitrary sequence of characters." 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.
Hope this helps!
这篇关于STL basic_string长度为空字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!