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

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -