[英]copying data from array to vector c++ c99
i have 2 classes working with communication data in one class the array information is stored as a fixed array 我有两类处理通讯数据,在一类中,数组信息存储为固定数组
uint8 msg[512];
//fixed array to reduce mem fragmentation on target
i want to memory copy this to a vector in the 2nd class 我想将其复制到第二类的向量中
vector<uint16> schbuff;
// not set offend so mem fragmentation on such a issue
i am using c++ c99 so and Im looking for a way not to have pass each element to the new container 我正在使用c ++ c99,所以我正在寻找一种方法来避免将每个元素传递给新容器
if they are both the same type it would not be such a issue but imm new to c++ so any help would be appreciated 如果它们都是相同的类型,那将不是问题,但是对于C ++来说是新的,所以将不胜感激
You can use std::copy
: 您可以使用
std::copy
:
#include <algorithm> // for copy
#include <iterator> // for begin, end, back_inserter
std::copy(std::begin(msg), std::end(msg), std::back_inserter(schbuff));
You can use constructor 您可以使用构造函数
vector<uint16> schbuff( msg, msg + 512 );
Or can use member function assign 或者可以使用成员函数分配
schbuff.assign( msg, msg + 512 );
Or can use member funcrion reserve. 或者可以使用成员功能储备。 For example
例如
vector<uint16> schbuff;
schbuff.reserve( 512 );
std::copy( msg, msg + 512, std::back_inserter( msg ) );
const int msg_size = 512;
uint8 msg[msg_size];
vector<uint8> msg_vec;
msg_vec.assign(msg, msg + msg_size);
Edit: just noticed the types are different. 编辑:只是注意到类型不同。 But I will keep the answer for example of how to put in vector.
但是我将保留例如如何放入向量的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.