组合 std::string 和 std::vector
这不是实际的代码,但这代表了我的问题。
std::string str1 = "head";
char *buffer = "body\0body"; // Original code has nullbytes;
std::string str2 = "foot";
std::vector<char> mainStr(buffer, buffer + strlen(buffer));
我想按顺序将 str1
和 str2
放入 mainStr
中:
头体\0bodyfoot
因此二进制数据被保留。可以这样做吗?
PS:感谢您告知 strlen
部分是错误的。我只是用它来表示缓冲区的长度。 :)
This is not the actual code, but this represents my problem.
std::string str1 = "head";
char *buffer = "body\0body"; // Original code has nullbytes;
std::string str2 = "foot";
std::vector<char> mainStr(buffer, buffer + strlen(buffer));
I want to put str1
and str2
to mainStr
in an order:
headbody\0bodyfoot
So the binary data is maintained. Is this possible to do this?
PS: Thanks for telling the strlen
part is wrong. I just used it to represent buffer
's length. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
应该有某种方法来定义“缓冲区”中数据的长度。
通常,字符 0 用于此目的,并且大多数标准文本函数都采用这一点。因此,如果您将字符 0 用于其他目的,则必须提供另一种方法来找出数据长度。
举个例子:
这里我们使用数组,因为它提供了比指针更多的信息——存储数据的大小。
There should be some way of defining length of data in "buffer".
Usually character 0 is used for this and most of standard text functions assume this. So if you use character 0 for other purposes, you have to provide another way to find out length of data.
Just for example:
Here we use array because it provides more information that a pointer - size of stored data.
您不能使用
strlen
,因为它使用 '\0' 来确定字符串的结尾。但是,以下内容将满足您的要求:因为字符缓冲区在字符串末尾添加了一个 NUL 字符,所以您需要忽略它(因此最后一个迭代器中的 -1 )。
You cannot use
strlen
as it uses '\0' to determine the end of string. However, the following will do what you are looking for:Because the character buffer adds an NUL character at the end of the string, you'll want to ignore it (hence the -1 from the last iterator).
顺便提一句。如果字符串中有 nul 字节,
strlen
将不起作用!要插入向量的代码是:
front:
back:
使用上面的代码(使用
strlen
将打印)headbodyfoot
编辑:只需将
copy
更改为insert<我认为 /code> 作为
copy
需要可用空间。btw.
strlen
will not work if there are nul bytes in your string!The code to insert into the vector is:
front:
back:
With your code above (using
strlen
will print)headbodyfoot
EDIT: just changed the
copy
toinsert
ascopy
requires the space to be available I think.您可以使用
std::vector::insert
将所需的数据附加到mainStr
中。像这样的东西:
免责声明:我没有编译它。
You could use
std::vector<char>::insert
to append the data you need intomainStr
.Something like this:
Disclaimer: I didn't compile it.
您可以使用 IO 流。
You can use IO streams.
str1 和 str2 是写入文本的字符串对象。
我希望编译器在诸如缓冲区声明之类的语句上失败,并且我不关心它破坏了多少遗留代码。如果您仍在构建它,您仍然可以修复它并放入 const。
您需要更改向量的声明,因为 strlen 将在第一个空字符处停止。如果你
这样做了,那么 sizeof(buffer) 实际上会给你接近你想要的东西,尽管你也会得到最后的空终止符。
一旦你的向量 mainStr 设置正确,你就可以这样做:
如果向量是使用缓冲区设置的,则 buffer+sizeof(buffer)-1
str1 and str2 are string objects that write the text.
I wish compilers would fail on statements like the declaration of buffer and I don't care how much legacy code it breaks. If you're still building it you can still fix it and put in a const.
You would need to change your declaration of vector because strlen will stop at the first null character. If you did
then sizeof(buffer) would actually give you close to what you want although you'll get the end null-terminator too.
Once your vector mainStr is then set up correctly you could do:
if vector was set up using buffer, buffer+sizeof(buffer)-1