How can I insert element into beginning of vector?

C++Visual C++VectorStd

C++ Problem Overview


I need to insert values into the beginning of a std::vector and I need other values in this vector to be pushed to further positions for example: something added to beginning of a vector and values moved from position 1 to 2, from 2 to 3 etc.

How can I do that?

C++ Solutions


Solution 1 - C++

Use the std::vector::insert function accepting an iterator to the first element as a target position (iterator before which to insert the element):

#include <vector>

int main() {
	std::vector<int> v{ 1, 2, 3, 4, 5 };
	v.insert(v.begin(), 6);
}

Alternatively, append the element and perform the rotation to the right:

#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v{ 1, 2, 3, 4, 5 };
    v.push_back(6);
    std::rotate(v.rbegin(), v.rbegin() + 1, v.rend());
}

Solution 2 - C++

You should consider using std::deque. It works alot like a std::vector but you can add and remove items from both the front and the end.

It does this by dividing the internal storage up into smaller blocks. You still have random-access iterators with good lookup speed.

If your container is small it should be fine to use the std::vector approach but if you are storing large amounts of data the std::deques performance for inserting/deleting at the front will be far superior.

Solution 3 - C++

What about this?

#include <vector>
#include <algorithm>
#include <iterator>

int main()
{ 
    std::vector<int> v1 = { 1, 2, 3 };
    std::vector<int> v2 = { 4, 5, 6 };

    // merge
    std::vector<int> dst;
    std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(dst));
}

Finally dst is: {1, 2, 3, 4, 5, 6}

Note: It is a sample, so you should check if v1 is empty etc.

Solution 4 - C++

You can insert values to std::vector from back and then use std::reverse:

Example:

#include <vector>
#include <algorhitm>
#include <iostream>

void printVector( std::vector< int > const & _vector )
{
    for( auto value : _vector )
    {
         std::cout << value << " ";
    }

    std::cout << std::endl;
}

int main()
{
    std::vector< int > someVec;
    
    someVec.push_back( 5 );
    someVec.push_back( 4 );
    someVec.push_back( 3 );
    someVec.push_back( 2 );
    someVec.push_back( 1 );

    // (1)
    printVector( someVec );

    std::reverse( someVec.begin(), someVec.end() );
    
    // (2)
    printVector( someVec );

    return 0;
}

Output (1):

5 4 3 2 1 

Output (2):

1 2 3 4 5 

Solution 5 - C++

You may try this

    vector<int> v={1,2,3,4,5};
    for(int i=0;i<5;i++){
        v.insert(v.begin(),i+1);
    }

Output is {5,4,3,2,1,1,2,3,4,5}

Every element is shifted to the right after insertion

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaweł SzymkowiczView Question on Stackoverflow
Solution 1 - C++RonView Answer on Stackoverflow
Solution 2 - C++superView Answer on Stackoverflow
Solution 3 - C++75ntamasView Answer on Stackoverflow
Solution 4 - C++Yevhenii MamontovView Answer on Stackoverflow
Solution 5 - C++Erik NouroyanView Answer on Stackoverflow