c++ - Copy struct to vector -
say have structure of various data types, want copy each single byte in vector. this:
vector<unsigned char> myvector; // vector unsigned char buf[sizeof mystructure]; // array memcpy(&buf, &mystructure, sizeof mystructure); // copy struct array myvector.insert(myvector.begin(), buf, buf + sizeof mystructure); // copy array vector
is there quickest way allows me copy struct mystruct
vector myvector
without passing through array buf
?
you can try iterator-pair constructor:
auto const ptr = reinterpret_cast<unsigned char*>(&mystructure); std::vector<unsigned char> myvector( ptr, ptr + sizeof mystructure );
Comments
Post a Comment