c++ - Feed std::vector<unsigned char> from unsigned char * -
i have write piece of code:
unsigned char *buffer = ... ... std::vector<unsigned char> vec(buffer,128);
this works feed vector after declaration (suppose vector field of class)
unsigned char *buffer = ... ... std::vector<unsigned char> vec; ... vec = vec(buffer,128) ???
i not know on last line. thing works resize vector memcpy. there better way?
well, move semantics, can do
vec = std::vector<unsigned char>(buffer, buffer + 128);
if ruffles feathers, can use std::copy
std::back_inserter
:
vec.reserve(128); std::copy(buffer, buffer+128, std::back_inserter(vec));
another option use vector::assign
.
Comments
Post a Comment