I have a scenario in which I need to copy the contents of a raw dynamically allocated uint8_t
array into a vector (which is guaranteed to be empty whenever this scenario happens).
vector<uint8_t> myVector;
const uint8_t* myRawArray;
It is really important to me that the copy operation is as efficient as possible and portable (various compiler versions might be used).
One approach I thought of using is this:
myVector.reserve(byteCount);
myVector.insert(myVector.begin(), myRawArray, myRawArray + byteCount);
Any ideas on how the speed of that compares to this one:
myVector.resize(byteCount);
memcpy(myVector.data(), myRawArray, byteCount);
I guess memcpy
should be fast but then I am forced to use resize
which needs to zero-out the memory, so I guess it will slow it down a bit..
Also, any other suggestions?