C ++ - 分配一个无符号字符缓冲区,然后用一个字符串填充 [英] C++ - Allocate an unsigned char buffer and then fill it with a string
问题描述
我realively新 C ++
所以请原谅我,如果这是一个幼稚的问题 - 但我卡上找到答案
我想创建一个 unsigned char型
大小的数组 1024
我已经用下面的$ C完成$ C:
无符号字符* r_record =新unsigned char型[1024]();
现在我有一个的std ::字符串
变量:
的std ::字符串HW =你好词;
和我想填充 r_record
与硬件
(即世界您好')开始于 10'th
字节。
如何可以将硬件
到 r_record
?
因此,实际上,我r_record数据看起来像(其中
的都是空的。)
[.........世界您好......等等]
您可以使用的 的std ::复制
,从算法
标题:
的std ::复制(hw.begin(),hw.end(),r_record + 10);
如果你想使用一个矢量,而不是动态分配的数组(好主意),那么
的std ::矢量< unsigned char型> r_record(1024); // 1024初始化为零元素
性病::复制(hw.begin(),hw.end(),r_record.begin()+ 10);
I am realively new to C++
so please forgive me if this is a naive question - but I'm stuck on finding an answer.
I am trying to create an unsigned char
array of size 1024
which I have done with the following code:
unsigned char *r_record = new unsigned char[1024]();
Now I have an std::string
variable:
std::string hw = "Hello Word";
And I would like to populate the r_record
with hw
(i.e., 'Hello World') starting at the 10'th
byte.
How can I place hw
into r_record
?
So in effect, my r_record data would look like (where the .
's are empty):
[.........Hello World......and so on]
You can use std::copy
, from the algorithm
header:
std::copy(hw.begin(), hw.end(), r_record + 10);
If you want to use a vector instead of the dynamically allocated array (a good idea), then
std::vector<unsigned char> r_record(1024); // 1024 zero initialized elements
std::copy(hw.begin(), hw.end(), r_record.begin() + 10);
这篇关于C ++ - 分配一个无符号字符缓冲区,然后用一个字符串填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!