Pregunta

Let's take an unsigned int as an example. So, I want to add an unsigned int to an std::vector<char> such that the next four bytes of the vector will be populated by the four bytes of the uint.

Now, I'd like to do it using Sharptooth's answer here but I have two questions:

  1. How can I use this method to insert the uint starting at the first free location of the vector - where the end iterator points to.
  2. I wonder if the end iterator is incremented when copying into a vector using memcpy which isn't vector's function.

If the answer of 2 is negative, what method would you recommend as the best way to accomplish this task. The other answers in the mentioned link refer to adding to the beginning of the vector.

The question refers to other PODs as well.

¿Fue útil?

Solución

Adjusting the answer you linked:

myVector.insert(myVector.end(), begin_binary(num), end_binary(num));

Where the begin and end functions just cast the value you want to insert. Doing it this way does increase the size of the vector (and its capacity, if necessary).

Otros consejos

1,2

std::copy(begin_binary(num), end_binary(num), std::back_inserter(my_vector));

STL automatically manages vector size while inserting new elements.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top