The Wayback Machine - https://web.archive.org/web/20230403075427/https://www.geeksforgeeks.org/vector-insert-function-in-c-stl/
Skip to content
Related Articles
Open in App
Not now

Related Articles

vector insert() function in C++ STL

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 28 Mar, 2023
Improve Article
Save Article
Like Article

std::vector::insert() is a built-in function in C++ STL that inserts new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted.

Time Complexity – Linear O(N)

  1. Syntax:
vector_name.insert (position, val)
  1. Parameter: The function accepts two parameters specified as below:
    • position – It specifies the iterator which points to the position where the insertion is to be done.
    • val – It specifies the value to be inserted.

C++




// A program to illustrate the function of
// vector_name.insert(position,val)
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // Initialising the vector
    vector<int> vector_name{ 1, 2, 3, 4, 5 };
 
    // Printing out the original vector
    cout << "Original vector :\n";
    for (auto x : vector_name)
        cout << x << " ";
    cout << "\n";
 
    // Inserting the value 100 at position 3(0-based
    // indexing) in the vector
    vector_name.insert(vector_name.begin() + 3, 100);
 
    // Printing the modified vector
    cout << "Vector after inserting 100 at position 3 :\n";
    for (auto x : vector_name)
        cout << x << " ";
    cout << "\n";
 
    // Inserting the value 500 at position 1(0-based
    // indexing) in the vector
    vector_name.insert(vector_name.begin() + 1, 500);
 
    // Printing the modified vector
    cout << "Vector after inserting 500 at position 1 :\n";
    for (auto x : vector_name)
        cout << x << " ";
    return 0;
}
 
// This code is contributed by Abhijeet Kumar(abhijeet19403)

Output

Original vector :
1 2 3 4 5 
Vector after inserting 100 at position 3 :
1 2 3 100 4 5 
Vector after inserting 500 at position 1 :
1 500 2 3 100 4 5 

       2. Syntax:

vector_name.insert(position, size, val)
  1. Parameter: The function accepts three parameters specified as below:
    • position – It specifies the iterator which points to the position where the insertion is to be done.
    • size – It specifies the number of times a val is to be inserted at the specified position.
    • val – It specifies the value to be inserted.

C++




// A program to illustrate the function of
// vector_name.insert(position,size,val)
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // Initialising the vector
    vector<int> vector_name{ 1, 2, 3, 4, 5 };
 
    // Printing out the original vector
    cout << "Original vector :\n";
    for (auto x : vector_name)
        cout << x << " ";
    cout << endl;
 
    // Inserting the value 100, 4 times starting at position 3
    vector_name.insert(vector_name.begin() + 3,4, 100);
 
    // Printing the modified vector
    cout << "Vector after inserting 100, 4 times, starting at position 3 :\n";
    for (auto x : vector_name)
        cout << x << " ";
    return 0;
}
 
// This code contributed by Harsh Singh (hsnooob)

Output

Original vector :
1 2 3 4 5 
Vector after inserting 100, 4 times, starting at position 3 :
1 2 3 100 100 100 100 4 5 

      3. Syntax:

vector_name.insert(position, iterator1, iterator2)
  1. Parameter: The function accepts three parameters specified as below:
    • position – It specifies the position at which insertion is to be done in the vector.
    • iterator1 – It specifies the starting position from which the elements are to be inserted
    • iterator2 – It specifies the ending position till which elements are to be inserted

C++




// A program to illustrate the function of
// vector_name.insert(position,itr1,itr2)
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // Initialising the vector
    vector<int> original{ 1, 2, 3, 4, 5 };
 
    vector<int>temp{ 2,5,9,0,3,10};
 
    // Printing out the original vector
    cout << "Original vector :\n";
    for (auto x : original)
        cout << x << " ";
    cout << endl;
 
    // Inserting the portion of temp vector in original vector
    // temp.begin()+3 is staring iterator of vector to be copied
    // temp.begin()+5 is ending iterator of vector to be copied
    original.insert(original.begin() + 3, temp.begin()+2, temp.begin()+5);
 
    // Printing the modified vector
    cout << "Vector after Inserting the portion of temp vector in original vector :\n";
    for (auto x : original)
        cout << x << " ";
    return 0;
}
 
// This code contributed by Harsh Singh (hsnooob)

Output

Original vector :
1 2 3 4 5 
Vector after Inserting the portion of temp vector in original vector :
1 2 3 9 0 3 4 5 

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!