Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert char* to std::vector?

Tags:

c++

I have a char* variable:

// char* buffer; // ... fread (buffer, 1, lSize, pFile); // ... 

How can I convert it to std::vector? Casting will get me an error.

like image 331
Richard Knop Avatar asked Nov 24 '10 23:11

Richard Knop


People also ask

How do I cast an array to a vector?

To convert an array to vector, you can use the constructor of Vector, or use a looping statement to add each element of array to vector using push_back() function.


2 Answers

std::vector<char> data(buffer, buffer + size); 

James McNellis answer is better if you can do it like that.

like image 183
ronag Avatar answered Oct 02 '22 22:10

ronag


Why not use a std::vector to begin with:

std::vector<char> buffer(lSize); std::fread(&buffer[0], 1, buffer.size(), pFile); 
like image 27
James McNellis Avatar answered Oct 02 '22 23:10

James McNellis