Insert multiple values into vector

boost, c++, c++98

Solution

This is what I would probably do:

vector<T> copy;
for (vector<T>::iterator i=original.begin(); i!=original.end(); ++i)
{
    copy.push_back(*i);
    if (*i == first)
        copy.push_back(second);
}
original.swap(copy);

Put a call to reserve in there if you want. You know you need room for at least `original.size()` elements. You could also do an initial iteraton over the vector (or use `std::count`) to determine the exact amount of elements to reserve, but without testing, I don't know whether that would improve performance.

Problem

I have a `std::vector<T>` variable. I also have two variables of type T, the first of which represents the value in the vector after which I am to insert, while the second represents the value to insert. So lets say I have this container: `1,2,1,1,2,2` And the two values are 2 and 3 with respect to their definitions above. Then I wish to write a function which will update the container to instead contain: ``` 1,2,3,1,1,2,3,2,3 ``` I am using c++98 and boost. What std or boost functions might I use to implement this function? Iterating over the vector and using std::insert is one way, but it gets messy when one realizes that you need to remember to hop over the value you just inserted.

Original source