1

I have an array of unsigned chars ( unsigned char *), how can I convert it to a vector of unsigned chars? ( std::vector<unsigned char> )?

Waqar
  • 8,558
  • 4
  • 35
  • 43
Kidsm
  • 308
  • 2
  • 14
  • 3
    do you the size of your array? also check ouit this : https://stackoverflow.com/questions/33654056/stdarray-or-stdvector-from-pointer – Pierre Baret Jun 30 '20 at 15:51
  • 1
    Can you please attach your code? – ksohan Jun 30 '20 at 15:52
  • 2
    `vector vec(array, array + size);` as already said this copies the array to the vector not converts it. – john Jun 30 '20 at 15:53
  • @anastaciu The answer is the same regardless of type, and an array's name decays into a pointer to its first element meaning that is the same too. The difference here is that the size will have to be known/supplied differently, assuming the underlying array is dynamic. – underscore_d Jun 30 '20 at 16:12
  • 1
    If the size is not known, then it can't be done. – Waqar Jun 30 '20 at 16:19
  • 2
    Pointer to the array is groovy, that gives us the starting point, but where is the end? How long is the array? Can't do much safely without knowing where to stop copying. – user4581301 Jun 30 '20 at 16:23
  • @john Yeah, there's no way for a `vector` to take over ownership of an existing buffer, but "conversion" in C++ is widely understood to mean creating a new object anyway, so it's no problem. – underscore_d Jun 30 '20 at 16:33

0 Answers0