简体   繁体   中英

std::copy char vector byte by byte

How do I copy an int (or any other data type) byte-by-byte to a vector<unsigned char> ?

int x = 10;
std::vector<unsigned char> byteArray(sizeof(int));
std::copy(byteArray.begin(), byteArray.end(), x); // how do I use x here?

If not possible with std::copy , can it be done with memcpy or any other technique?

Problem you are dealing with is called serialization. Solution with memcpy is 'crude but effective' so if your project is small I think it is OK to use it. Otherwise I recommend looking at some solutions, for example, provided in boost or Qt. This will provide you with generic interface for multiple data types.

How do you serialize an object in C++?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM