阅读背景:

带空字符的STL c_basic_string长度

来源:互联网 

Why is it that you can insert a '

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 : length(str.c_str()),您会得到字符串的长度直到第一个'\0'字符为止?

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

1 个解决方案

#1


17  

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.

原因是c样式的字符串定义为以空字节结束的字节序列。当您使用.c_str()从c++ std:::string中获取C样式的字符串时,那么您将得到C+ string后面带有一个空字节的序列。当您将它传递给strlen时,它将扫描整个字节,直到它到达一个空字节,然后报告它在此之前找到了多少字符。如果字符串包含一个空字节,那么strlen将报告一个小于整个字符串长度的值,因为它将在到达字符串的真正末端之前停止。

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.

一个重要的细节是strlen和char_traits ::length不是同一个函数。然而,c++ ISO规范char_traits <图表> ::长度(§21.1.1)说,char_traits <图表> ::长度(s)返回最小的我这样char_traits <图表> ::情商(s[我],图表())是正确的。对于char_traits ,如果两个字符在做=比较时相等,那么eq函数就返回,并且通过写入char()来构造一个字符会产生一个空字节,这就等于说“字符串中的第一个空字节在哪里?”这就是strlen的工作原理,虽然这两个函数在技术上是不同的。

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.

一个c++ std::string,但是,它是“任意字符序列”的更一般的概念。它的实现细节是隐藏在外部世界之外的,尽管它可能由一个开始和停止指针或一个指针和一个长度表示。因为这种表示不依赖于存储的字符,所以询问std::string的长度告诉您有多少字符,而不管这些字符实际上是什么。

Hope this helps!

希望这可以帮助!


' char in a std::basic_string and the .length() method is unaffected but if you call Why is it that you can insert a '

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 : length(str.c_str()),您会得到字符串的长度直到第一个'\0'字符为止?

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

1 个解决方案

#1


17  

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.

原因是c样式的字符串定义为以空字节结束的字节序列。当您使用.c_str()从c++ std:::string中获取C样式的字符串时,那么您将得到C+ string后面带有一个空字节的序列。当您将它传递给strlen时,它将扫描整个字节,直到它到达一个空字节,然后报告它在此之前找到了多少字符。如果字符串包含一个空字节,那么strlen将报告一个小于整个字符串长度的值,因为它将在到达字符串的真正末端之前停止。

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.

一个重要的细节是strlen和char_traits ::length不是同一个函数。然而,c++ ISO规范char_traits <图表> ::长度(§21.1.1)说,char_traits <图表> ::长度(s)返回最小的我这样char_traits <图表> ::情商(s[我],图表())是正确的。对于char_traits ,如果两个字符在做=比较时相等,那么eq函数就返回,并且通过写入char()来构造一个字符会产生一个空字节,这就等于说“字符串中的第一个空字节在哪里?”这就是strlen的工作原理,虽然这两个函数在技术上是不同的。

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.

一个c++ std::string,但是,它是“任意字符序列”的更一般的概念。它的实现细节是隐藏在外部世界之外的,尽管它可能由一个开始和停止指针或一个指针和一个长度表示。因为这种表示不依赖于存储的字符,所以询问std::string的长度告诉您有多少字符,而不管这些字符实际上是什么。

Hope this helps!

希望这可以帮助!


' char in a




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: