Open In App

Vector insert() in C++ STL

Last Updated : 19 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, the vector insert() is a built-in function used to insert new elements at the given position in a vector. In this article, we will learn about the vector insert() function in C++.

 Let’s take a look at an example that shows the how to use this function:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 3, 7, 9};

    // Insert element 8 at index 2
    v.insert(v.begin() + 2, 8);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 3 8 7 9 

Explanation: The function inserts the element 8 at the 2nd index of vector.

Syntax of Vector insert()

The vector insert() is a member function of std::vector class defined inside <vector> header file and have 3 implementations:

C++
v.insert(pos, val);   // Insert single element 
v.insert(pos, n, val);   // Insert multiple copies of an element
v.insert(pos, {val1, val2, ...})   // Insert list of elements
v.insert(pos, first, last);   // Insert range of elements 

These implementations work on different cases which are as follows:

Insert an Element at Given Index

We can use the vector insert() function to insert the element at the given index. All the elements to the right of the given index will be shifted once place to the right to make space for new element.

Syntax

C++
v.insert (pos, val);

Parameters

  • val: Value to be inserted.
  • pos: Iterator to the position where val is to be inserted.

Return Value

  • Returns an iterator to the inserted element.

The insert() function is crucial for adding elements to a vector at specific positions.

Example

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 2, 4, 5};

    // Insert element 3 at index 2
    v.insert(v.begin() + 2, 3);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 5 

Explanation: The vector insert() function insert the element 3 at the 2nd index and all the elements to the right of the 2nd index will be shifted once place to the right to make space for new element.

Insert Multiple Copies of an Element

The vector insert() can also be used to insert multiple copies of an element at the given index.

Syntax

C++
v.insert(pos, n, val)

Parameters

  • val - Value to be inserted.
  • pos: Iterator to the position where val is to be inserted.
  • n: Number of times val is to be inserted.

Return Value

  • Returns an iterator that points to the first inserted element.
  • If no element is inserted, then returns iterator to the pos.

Example

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 3, 4, 5};

    // Inserting value 9, 3 times at index 2
    v.insert(v.begin() + 1, 3, 2);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 2 2 2 3 4 5 

Insert List of Elements

The vector insert() function can also insert elements from an initializer list at given index.

C++
v.insert(pos, {val1, val2, ...});

Parameters

  • pos- Iterator to the position where range is to be inserted.
  • {val1, val2, ...} - Initializer list containing values to insert.

Return Value

  • Returns an iterator that points to the first inserted element.
  • If no element is inserted, then returns iterator to the pos.

Example

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v{1, 2, 3,};

    // Inserting elements to v
    v.insert(v.begin() + 3, {4, 5});
  
    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 5 

Insert the Range of Elements

The vector insert() function can also insert elements from a range into the vector at given index. This range can be any STL container or an array.

C++
v.insert(pos, first, last)

Parameters

  • pos- Iterator to the position where range is to be inserted.
  • first- Iterator to the first element in the range from which the elements are to be inserted.
  • last- Iterator to the element one place after the last element in the range.

Return Value

  • Returns an iterator that points to the first inserted element.
  • If no element is inserted, then returns iterator to the pos.

Example

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v{1, 2, 3,};
    list<int> l{4, 5};

    // Inserting all elements of l to v
    v.insert(v.begin() + 3, l.begin(), l.end());
  
    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 5 

Vector insert() vs push_back()

The vector insert() and vector push_back() are both used to insert new elements in a vector, but they differ in the way they perform insertion. Following table lists the primary differences between vector insert() and vector push_back():

FeatureVector insert()Vector push_back()
PositionInserts at any specified position.Always appends at the end of the vector.
Number of ElementsCan insert single or multiple elements or a range.Inserts only one element at a time.
Element ShiftingShifts elements after the insertion point.No shifting of elements (added at the end).
Time ComplexityO(n)O(1)

Next Article
Practice Tags :

Similar Reads