C++ const char * to vector<unsigned char> 初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9799598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:18:03  来源:igfitidea点击:

const char * to vector<unsigned char> Initalisation

c++visual-c++vectorcharinitialization

提问by Mr Chris

I understand that using vector is a good way to store binary data when using C++ and the STL. However for my unit tests I'd like to initalise the vector using a const char* C string variable.

我知道在使用 C++ 和 STL 时,使用向量是存储二进制数据的好方法。但是,对于我的单元测试,我想使用 const char* C 字符串变量来初始化向量。

I'm attempting to use a variant of the code found here - Converting (void*) to std::vector<unsigned char>- to do this:

我正在尝试使用此处找到的代码的变体 - Converting (void*) to std::vector<unsigned char>- 来执行此操作:

const char* testdata = "the quick brown fox jumps over the lazy dog.";

unsigned char* buffer = (unsigned char*)testdata;
typedef vector<unsigned char> bufferType;

bufferType::size_type size = strlen((const char*)buffer);
bufferType vec(buffer, size);

However the VC++ compiler is not happy with the line initialising the vector, stating:

然而,VC++ 编译器对初始化向量的行不满意,声明:

error C2664: 'std::vector<_Ty>::vector(unsigned int,const _Ty &)' : cannot convert parameter 1 from 'char *' to 'unsigned int'

I appreciate the extreme n00bity of this question and am fully prepared for much criticism on the code above :)

我很欣赏这个问题的极端性,并已准备好接受对上述代码的大量批评:)

Thanks in advance, Chris

提前致谢,克里斯

回答by Luchian Grigore

It should be

它应该是

bufferType vec(buffer, buffer + size);

not

不是

bufferType vec(buffer, size);

回答by aldo

std::transformis useful for just this sort of problem. You can use it to "transform" one piece of data at a time. See documentation here:

std::transform仅对此类问题有用。您可以使用它一次“转换”一份数据。请参阅此处的文档:

http://www.cplusplus.com/reference/algorithm/transform/

http://www.cplusplus.com/reference/algorithm/transform/

The following code works in VS2010. (I created a std::stringfrom your const char*array, but you could probably avoid that if you really wanted to.)

以下代码适用于 VS2010。(我std::string从你的const char*数组中创建了一个,但如果你真的想要,你可以避免这种情况。)

#include <algorithm>
#include <vector>

int main(int, char*[])
{
  // Initial test data
  const char* testdata = "the quick brown fox jumps over the lazy dog.";

  // Transform from 'const char*' to 'vector<unsigned char>'
  std::string input(testdata);
  std::vector<unsigned char> output(input.length());
  std::transform(input.begin(), input.end(), output.begin(),
    [](char c)
    {
      return static_cast<unsigned char>(c);
    });

  // Use the transformed data in 'output'...


  return 0;
}

回答by serup

Here is what worked for me:

这是对我有用的:

// Fetch data into vector
std::vector<char> buffer = <myMethod>.getdata();

// Get a char pointer to the data in the vector
char* buf = buffer.data();

// cast from char pointer to unsigned char pointer
unsigned char* membuf = reinterpret_cast<unsigned char*>(buf);            

// now convert to vector<unsigned char> buffer
std::vector<unsigned char> vec(membuf, membuf + buffer.size()); 

// display vector<unsigned char>   
CUtils::<myMethodToShowDataBlock>(vec);      

回答by Jonathan Mee

What you intended to do seems to be something like:

你打算做的似乎是这样的:

buffertype vec(testdata, next(testdata, strlen(testdata)));

There is no need for the intermediate buffervariable. The conversion from charto unsigned charwill happen implicitly.

不需要中间buffer变量。从charto的转换unsigned char将隐式发生。

Note that this does not grab the terminating '\0'character from testdata. So if you wanted to be able to do something like: cout << vec.data()you wouldn't be able to. If you want that you could do: buffertype vec(testdata, next(testdata, strlen(testdata) + 1))or you may just want to consider doing:

请注意,这不会'\0'testdata. 因此,如果您希望能够执行以下操作:cout << vec.data()您将无法做到。如果你想要你可以这样做:buffertype vec(testdata, next(testdata, strlen(testdata) + 1))或者你可能只想考虑这样做:

basic_string<unsigned char> vec(testdata, next(testdata, strlen(testdata)));

Which will preserve a hidden '\0'. Because this is not a stringyou won't be able to do, cout << vecbut cout << vec.data()will work. I've created a Live Exampleof each of these.

这将保留一个隐藏的'\0'. 因为这不是string您无法做到的,cout << vec而是cout << vec.data()可以工作的。我已经创建了其中Live Example的每一个。