std::string 可以包含嵌入的空值吗?

发布于 2024-09-01 17:44:43 字数 103 浏览 18 评论 0原文

对于常规 C 字符串,空字符 '\0' 表示数据结束。

std::string 怎么样,我可以有一个嵌入空字符的字符串吗?

For regular C strings, a null character '\0' signifies the end of data.

What about std::string, can I have a string with embedded null characters?

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

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

发布评论

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

评论(4

你的他你的她 2024-09-08 17:44:43

是的,您可以在 std::string 中嵌入空值。

示例:

std::string s;
s.push_back('\0');
s.push_back('a');
assert(s.length() == 2);

注意:std::stringc_str() 成员始终会向返回的 char 缓冲区附加一个空字符;但是,std::stringdata() 成员可能会也可能不会将空字符附加到返回的 char 缓冲区。

小心operator+=

需要注意的一件事是不要在RHS 上将operator+=char* 一起使用。它只会累加到空字符为止。

例如:

std::string s = "hello";
s += "\0world";
assert(s.length() == 5);

正确的方式:

std::string s = "hello";
s += std::string("\0world", 6);
assert(s.length() == 11);

存储二进制数据更常见的是使用std::vector

一般情况下,更常见的是使用std::vector来存储任意二进制数据。

std::vector<char> buf;
buf.resize(1024);
char *p = &buf.front();

这可能更常见,因为 std::stringdata()c_str() 成员返回 const 指针,因此内存不可修改。使用 &buf.front() 您可以直接修改缓冲区的内容。

Yes you can have embedded nulls in your std::string.

Example:

std::string s;
s.push_back('\0');
s.push_back('a');
assert(s.length() == 2);

Note: std::string's c_str() member will always append a null character to the returned char buffer; However, std::string's data() member may or may not append a null character to the returned char buffer.

Be careful of operator+=

One thing to look out for is to not use operator+= with a char* on the RHS. It will only add up until the null character.

For example:

std::string s = "hello";
s += "\0world";
assert(s.length() == 5);

The correct way:

std::string s = "hello";
s += std::string("\0world", 6);
assert(s.length() == 11);

Storing binary data more common to use std::vector

Generally it's more common to use std::vector to store arbitrary binary data.

std::vector<char> buf;
buf.resize(1024);
char *p = &buf.front();

It is probably more common since std::string's data() and c_str() members return const pointers so the memory is not modifiable. with &buf.front() you are free to modify the contents of the buffer directly.

止于盛夏 2024-09-08 17:44:43

是的。 std::string 只是一个具有优点的 vector

然而,在将这样的野兽传递给调用 .c_str() 并在 0 处停止的东西时要小心。

Yes. A std::string is just a vector<char> with benefits.

However, be careful about passing such a beast to something that calls .c_str() and stops at the 0.

静谧幽蓝 2024-09-08 17:44:43

你可以,但你为什么要这么做?在 std::string 中嵌入 NUL 只会带来麻烦,因为传递 std::string 的函数很可能会使用它的 c_str() 成员,并且大多数人会假设第一个 NUL 指示字符串的结尾。因此,这不是一个好主意。另请注意,在 UTF-8 中,只有“\0”会导致 0,因此即使出于 i18n 目的,也没有理由嵌入 NUL。

You can, but why would you want to? Embedding NUL in an std::string is just asking for trouble, because functions to which you pass an std::string may very well use it's c_str() member, and most will assume that the first NUL indicates the end of the string. Hence this is not a good idea to do. Also note that in UTF-8, only '\0' will result in a 0, so even for i18n purposes, there is no justification for embedding NULs.

醉南桥 2024-09-08 17:44:43

是的,这是有效的。

字符串中间可以有一个空字符。

但是,如果您使用中间带有空字符的 std::string 和 ac 字符串
在未定义行为小镇中发挥你的作用 - 没有人愿意在那里!!!:

 int n = strlen( strWithNullInMiddle.c_str() ); // Boom!!!

Yep this is valid.

You can have a null character in the middle of the string.

However, if you use a std::string with a null character in the middle with a c string
function your in undefined behaviour town - and nobody wants to be there!!!:

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