[英]Copy vector<char> into char*
I'm just studying C and C++ programming. 我只是在学习C和C ++编程。
I've searched and can't seem to find an answer that has a decent response. 我已经搜索过,但似乎找不到答案不错的答案。 Of course using <string>
is much easier but for this task I am REQUIRED to use only clib <string.h>
functions; 当然,使用<string>
要容易得多,但是对于此任务,我仅需要使用clib <string.h>
函数; I'm also not allowed to use C++11 functions. 我也不允许使用C ++ 11函数。
I have the 2 variables below, and want to move the contents of buffer
into c
. 我下面有2个变量,并想将buffer
的内容移入c
。
vector<char> buffer;
char* c = "";
How can I do this easily? 我如何轻松做到这一点?
I have this so far but it obviously doesn't work, otherwise I wouldn't be here. 到目前为止,我已经知道了,但是显然不起作用,否则我就不会在这里。
for (int b = 0; b < buffer.size(); b++)
{
c += &buffer[b];
}
The simplest way I can think of is; 我能想到的最简单的方法是:
std::vector<char> buffer;
// some code that places data into buffer
char *c = new char[buffer.size()];
std::copy(buffer.begin(), buffer.end(), c);
// use c
delete [] c;
std::copy()
is available in the standard header <algorithm>
. std::copy()
在标准头文件<algorithm>
可用。
This assumes the code that places data into buffer
explicitly takes care of inserting any trailing characters with value zero ( '\\0'
) into the buffer. 假设将数据显式放入buffer
的代码负责将任何值为零( '\\0'
)的尾随字符插入缓冲区。 Without that, subsequent usage of c
cannot assume the presence of the '\\0'
terminator. 否则, c
后续用法将不能假定存在'\\0'
终止符。
If you want to ensure a trailing '\\0'
is present in c
even if buffer
does not contain one, then one approach is; 如果你想确保尾随'\\0'
出现在c
即使buffer
不包含一个,再一个办法是;
std::vector<char> buffer;
// some code that places data into buffer
char *c = new char[buffer.size() + 1]; // additional room for a trailing '\0'
std::copy(buffer.begin(), buffer.end(), c);
c[buffer.size()] = '\0';
// use c
delete [] c;
One could also be sneaky and use another vector; 一个人也可以偷偷摸摸,使用另一种媒介。
std::vector<char> container;
// some code that places data into buffer
std::vector<char> v(container); // v is a copy of container
v.push_back('\0'); // if we need to ensure a trailing '\0'
char *c = &v[0]
// use c like a normal array of char
As long as the code that uses c
does not do anything that will resize v
, the usage of c
in this case is exactly equivalent to the preceding examples. 只要使用c
的代码不执行任何可调整v
大小的操作,则在这种情况下c
的用法与前面的示例完全相同。 This has an advantage that v
will be released when it passes out of scope (no need to remember to delete
anything) but a potential disadvantage that c
cannot be used after that point (since it will be a dangling pointer). 这样做的好处是v
超出范围时将被释放(无需记住delete
任何内容),但潜在的缺点是c
在该点之后不能再使用(因为它将是一个悬空的指针)。
First, allocate space for the data by assigning c = new char[buffer.size()];
首先,通过分配c = new char[buffer.size()];
为数据分配空间c = new char[buffer.size()];
Then use memcpy
to copy the data: memcpy(c, buffer.data(), buffer.size())
然后使用memcpy
复制数据: memcpy(c, buffer.data(), buffer.size())
Your for
loop would work in place of memcpy
, too. 您的for
循环也可以代替memcpy
起作用。
Also note that if vector<char>
stays in place all the time when you use char*
, and you are allowed to change the content of the vector, you could simply use the data behind the vector with a simple assignment, like this: 还要注意,如果在使用char*
时vector<char>
始终保持在原位,并且允许您更改vector的内容,则可以通过简单的分配简单地使用vector后面的数据,如下所示:
char *c = buffer.data();
I'm noticing some weird behavior when I create my char* of the given size is that it creates it bigger with some random
"hereýýýý««««««««"
values after my word 当我创建给定大小的char *时,我注意到一些怪异的行为,即它使我的单词后面带有一些随机的"hereýýýý««««««««"
值而使它变大
It looks like you do need a null-terminated C string after all. 看来您确实需要一个以N结尾的C字符串。 In this case you need to allocate one extra character at the end, and set it to zero: 在这种情况下,您需要在末尾分配一个额外的字符,并将其设置为零:
char *c = new char[buffer.size()+1];
memcpy(c, buffer.data(), buffer.size());
c[buffer.size()] = 0;
You can do it in this way: 您可以通过以下方式进行操作:
vector<char> buffer;
//I am assuming that buffer has some data
char *c = new char[buffer.size()+1];
for( int i=0; i<buffer.size(); i++ )
c[i] = buffer[i];
c[i] = '\0';
buffer.clear();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.