gpt4 book ai didi

c++ - 使用 << 或 >> 连接一个无符号字符数组

转载 作者:行者123 更新时间:2023-11-30 03:28:44 25 4
gpt4 key购买 nike

如果我有这两个数组:

unsigned char bytes1[n];
unsigned char bytes2[m];

(nm 是任意整数)我这样做:

cout << bytes1 << bytes2;

我设法显示这 2 个数组的内容。是否可以使用这个概念来连接这两个数组?类似的东西:

usigned char bytes1[2];
unsigned char bytes2[3];
unsigned char * bytes3 = new unsigned char[5];
bytes3 << bytes1 << bytes2;

最后,bytes2应该依次是bytes1bytes2的内容。

最佳答案

您可能想使用 stringstream在这种情况下。

此处引用: http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream

在标题中:

#include <sstream>

在正文中:

unsigned char bytes1[2];
unsigned char bytes2[3];
unsigned char * bytes3 = new unsigned char[5];

std::stringstream ss;
ss << bytes1 << bytes2;
ss >> bytes3

//This should be also good
std::string bytes3 = ss.str();

您的原始代码不起作用,因为 byte3不是 stream (特别是 ostream),所以它没有 <<运算符

关于c++ - 使用 << 或 >> 连接一个无符号字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46333349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com