문제

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.

도움이 되었습니까?

해결책

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).

다른 팁

1,2

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

STL automatically manages vector size while inserting new elements.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top