How to copy span of chars from char array to vector ? Starting index is always 0 and goes to some passed value x. I have char* buffer on heap ( size is 32*1024) and I am using that buffer to receive messages and set received message size in variable x. How to copy from 0th to xth char to vector<char> mainBuffer ?
( I can simply iterate but it looks inefficient way if message is long. Another way is like below but then I always in every pass create new vector)
char* buffer = new char[32*1024];
int x;
std::vector<std::vector<char> > mainBuffer;
//:loop
// here is some code where I recive message in buffer and set x
mainBuffer.push_back(std::vector<char>(buffer,buffer+x));
//:end loop
Does anyone know more efficient and elegant way to do this ?
With C++11, do:
This forwards the parameters and constructs the object directly inside mainBuffer.
Edit: With C++ (old), use std::swap:
Edit 2: If you can, call reserve on mainBuffer before using push_back on it.