STL basic_string 长度(含空字符)

发布于 2024-10-23 11:48:54 字数 365 浏览 2 评论 0原文

为什么您可以在 std::basic_string 中插入 '\0' 字符并且 .length() 方法不受影响,但如果您调用 char_traits::length(str.c_str())< /code> 你得到了直到第一个 '\0' 字符为止的字符串长度吗?

例如

string str("abcdefgh");
cout << str.length(); // 8
str[4] = '\0';
cout << str.length(); // 8
cout << char_traits<char>::length(str.c_str()); // 4

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗恋未遂 2024-10-30 11:48:54

好问题!

原因是 C 样式字符串被定义为以空字节结尾的字节序列。当您使用 .c_str() 从 C++ std::string 中获取 C 样式字符串时,您将返回 C++ 字符串存储的序列其后有一个空字节。当您将其传递给 strlen 时,它将扫描字节,直到遇到空字节,然后报告在此之前找到的字符数。如果 string 包含空字节,则 strlen 将报告一个小于字符串整个长度的值,因为它会在到达字符串的真正末尾之前停止。

一个重要的细节是 strlenchar_traits::length 不是同一个函数。但是,char_traits::length (§21.1.1) 的 C++ ISO 规范规定 char_traits::length(s) 返回 最小 i 使得 char_traits::eq(s[i], charT()) 为真。对于 char_traitseq 函数仅通过执行 == 比较来返回两个字符是否相等,并通过以下方式构造字符编写 char() 会产生一个空字节,因此这相当于说“字符串中的第一个空字节在哪里?”这本质上就是 strlen 的工作原理,尽管这两个函数在技术上是不同的。

然而,C++ std::string 是“任意字符序列”的更通用概念。其实现的细节对外界是隐藏的,尽管它可能由开始和停止指针或指针和长度表示。由于这种表示形式不依赖于存储的字符,因此询问 std::string 的长度可以告诉您有多少个字符,而不管这些字符实际上是什么。

希望这有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文