0

If I have a vector std::vector<int64_t> oldData, can I use move semantics to move the data into another vector std::vector<uint8_t> newData.

Instead of doing:

std::vector<uint8_t> newData(oldData.begin(),oldData.end()); 

Could I do

std::vector<uint8_t> newData = std::move(oldData);

Will this actually move the data instead of copying it and be more performant?

  • You could do `uint8_t* newData = reinterpret_cast(oldData.data());` if you just want to treat the data in the old vector as an array of bytes. – NathanOliver Mar 30 '20 at 13:28
  • Can't be done! note that `oldData` is an array of type `int64_t`. – NadavS Mar 30 '20 at 14:12

1 Answers1

1

No. If you think carefully about it, it's impossible - you to transform an array of 64 bit integers to an array of 8 bit integers, so you'll have to allocate new space for the new array (8 times smaller) and then copy the least significant 8 bits of every integer in the old array.

NadavS
  • 728
  • 2
  • 12