lundi 14 juin 2021

Converting std::vector

I have an std::vector<float> with N objects inside it. I am trying to see what is the most elegent approach that doesn't use for loops to convert it to a binary string.

I have come up with something but I am not sure if its very efficient

  const std::vector<uint8_t>& data = image_frame.data;
  const unsigned char* bytes = reinterpret_cast<const unsigned char*>(&data[0]);
  std::vector<unsigned char> byteVec(bytes, sizeof(uint8_t) * data.size()); // Does this make a copy? Is this size i set correct?
  std::string s(byteVec.begin(), byteVec.end()); // Does this make a copy?

I am not sure about the reverse though

Aucun commentaire:

Enregistrer un commentaire