add a new item to vector and shift it remaining part to right

c++

Solution

Check the vector::insert() function.

vector<int> vec ;

// Add elements to vec
vec.insert(vec.begin() + position, new_item);

Problem

I am trying to put a new item to vector, and shift remaining items. How can I do that ? Ex ``` vector ------------------------------------------------------- | 1 | 2 | 3 | 4 | 5 | 9 | 10 | 15 | 21 | 34 | 56 | 99 | ------------------------------------------------------- ^ new item = 14, it should be added to ^ After insertion, vector ------------------------------------------------------------ | 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 21 | 34 | 56 | 99 | ------------------------------------------------------------ ^ ^ ^-shifted to right by one-^ ```

Original source