组合 std::string 和 std::vector

发布于 2024-10-10 08:19:23 字数 471 浏览 0 评论 0原文

这不是实际的代码,但这代表了我的问题。

std::string str1 = "head";
char *buffer = "body\0body"; // Original code has nullbytes;
std::string str2 = "foot";
std::vector<char> mainStr(buffer, buffer + strlen(buffer));

我想按顺序将 str1str2 放入 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 技术交流群。

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

发布评论

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

评论(7

欲拥i 2024-10-17 08:19:23

应该有某种方法来定义“缓冲区”中数据的长度。
通常,字符 0 用于此目的,并且大多数标准文本函数都采用这一点。因此,如果您将字符 0 用于其他目的,则必须提供另一种方法来找出数据长度。

举个例子:

char buffer[]="body\0body";
std::vector<char> mainStr(buffer,buffer+sizeof(buffer)/sizeof(buffer[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:

char buffer[]="body\0body";
std::vector<char> mainStr(buffer,buffer+sizeof(buffer)/sizeof(buffer[0]));

Here we use array because it provides more information that a pointer - size of stored data.

一页 2024-10-17 08:19:23

您不能使用 strlen,因为它使用 '\0' 来确定字符串的结尾。但是,以下内容将满足您的要求:

std::string head = "header";
std::string foot = "footer";
const char body[] = "body\0body";

std::vector<char> v;
v.assign(head.begin(), head.end());
std::copy(body, body + sizeof(body)/sizeof(body[0]) - 1, std::back_inserter<std::vector<char> >(v));
std::copy(foot.begin(), foot.end(), std::back_inserter<std::vector<char> >(v));

因为字符缓冲区在字符串末尾添加了一个 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:

std::string head = "header";
std::string foot = "footer";
const char body[] = "body\0body";

std::vector<char> v;
v.assign(head.begin(), head.end());
std::copy(body, body + sizeof(body)/sizeof(body[0]) - 1, std::back_inserter<std::vector<char> >(v));
std::copy(foot.begin(), foot.end(), std::back_inserter<std::vector<char> >(v));

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).

琉璃梦幻 2024-10-17 08:19:23

顺便提一句。如果字符串中有 nul 字节,strlen 将不起作用!

要插入向量的代码是:

front:

mainStr.insert(mainStr.begin(), str1.begin(), str1.end());

back:

mainStr.insert(mainStr.end(), str2.begin(), str2.end());

使用上面的代码(使用 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:

mainStr.insert(mainStr.begin(), str1.begin(), str1.end());

back:

mainStr.insert(mainStr.end(), str2.begin(), str2.end());

With your code above (using strlen will print)

headbodyfoot

EDIT: just changed the copy to insert as copy requires the space to be available I think.

北笙凉宸 2024-10-17 08:19:23

您可以使用 std::vector::insert 将所需的数据附加到 mainStr 中。

像这样的东西:

std::string str1 = "head";
char buffer[] = "body\0body"; // Original code has nullbytes;
std::string str2 = "foot";

std::vector<char> mainStr(str1.begin(), str1.end());
mainStr.insert(mainStr.end(), buffer, buffer + sizeof(buffer)/sizeof(buffer[0]));
mainStr.insert(mainStr.end(), str2.begin(), str2.end());

免责声明:我没有编译它。

You could use std::vector<char>::insert to append the data you need into mainStr.

Something like this:

std::string str1 = "head";
char buffer[] = "body\0body"; // Original code has nullbytes;
std::string str2 = "foot";

std::vector<char> mainStr(str1.begin(), str1.end());
mainStr.insert(mainStr.end(), buffer, buffer + sizeof(buffer)/sizeof(buffer[0]));
mainStr.insert(mainStr.end(), str2.begin(), str2.end());

Disclaimer: I didn't compile it.

甚是思念 2024-10-17 08:19:23

您可以使用 IO 流。

std::string str1 = "head";
const char *buffer = "body\0body"; // Original code has nullbytes;
std::string str2 = "foot";
std::stringstream ss;
ss.write(str1.c_str(), str1.length())
  .write(buffer, 9) // insert real length here
  .write(str2.c_str(), str2.length());
std::string result = ss.str();

std::vector<char> vec(result.c_str(), result.c_str() + result.length());

You can use IO streams.

std::string str1 = "head";
const char *buffer = "body\0body"; // Original code has nullbytes;
std::string str2 = "foot";
std::stringstream ss;
ss.write(str1.c_str(), str1.length())
  .write(buffer, 9) // insert real length here
  .write(str2.c_str(), str2.length());
std::string result = ss.str();

std::vector<char> vec(result.c_str(), result.c_str() + result.length());
忱杏 2024-10-17 08:19:23

str1 和 str2 是写入文本的字符串对象。

我希望编译器在诸如缓冲区声明之类的语句上失败,并且我不关心它破坏了多少遗留代码。如果您仍在构建它,您仍然可以修复它并放入 const。

您需要更改向量的声明,因为 strlen 将在第一个空字符处停止。如果你

char buffer[] = "body\0body";

这样做了,那么 sizeof(buffer) 实际上会给你接近你想要的东西,尽管你也会得到最后的空终止符。

一旦你的向量 mainStr 设置正确,你就可以这样做:

std::string strConcat;
strConcat.reserve( str1.size() + str2.size() + mainStr.size() );
strConcat.assign(str1);
strConcat.append(mainStr.begin(), mainStr.end());
strConcat.append(str2);

如果向量是使用缓冲区设置的,则 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

char buffer[] = "body\0body";

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:

std::string strConcat;
strConcat.reserve( str1.size() + str2.size() + mainStr.size() );
strConcat.assign(str1);
strConcat.append(mainStr.begin(), mainStr.end());
strConcat.append(str2);

if vector was set up using buffer, buffer+sizeof(buffer)-1

欲拥i 2024-10-17 08:19:23
 mainStr.resize(str1.length() + str2.length() + strlen(buffer));  
 memcpy(&mainStr[0], &str1[0], str1.length());  
 memcpy(&mainStr[str1.length()], buffer, strlen(buffer));  
 memcpy(&mainStr[str1.length()+strlen(buffer)], &str2[0], str2.length());
 mainStr.resize(str1.length() + str2.length() + strlen(buffer));  
 memcpy(&mainStr[0], &str1[0], str1.length());  
 memcpy(&mainStr[str1.length()], buffer, strlen(buffer));  
 memcpy(&mainStr[str1.length()+strlen(buffer)], &str2[0], str2.length());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文