Vector insert() in C++ STL

Last Updated : 2 Jun, 2026

The vector::insert() function in C++ STL is used to insert one or more elements at a specified position in a vector. It automatically shifts existing elements to create space for the new elements while preserving their relative order.

  • Inserts elements at any valid position in a vector
  • Supports inserting a single element, multiple copies, initializer lists, and ranges
  • Returns an iterator pointing to the first inserted element
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

  • v.begin() + 2 points to the element at index 2.
  • insert() places 8 before that position.
  • Elements 7 and 9 are shifted one position to the right.
  • The resulting vector becomes {1, 3, 8, 7, 9}.

Syntax

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

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

Parameters:

  • pos - Iterator specifying the insertion position.
  • val - Value to be inserted.
  • n - Number of copies to insert.
  • first, last - Iterators defining the range of elements to insert.

Return Value: Returns an iterator pointing to the first inserted element.

Different Ways to Use vector::insert()

The vector::insert() function provides multiple overloads that allow inserting elements in different ways depending on the requirement. The following are the commonly used forms of insert().

Insert an Element at Given Index

The insert() function can be used to insert a single element at any valid position in a vector. All elements at and after the insertion position are shifted one place to the right.

Syntax

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.

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

  • v.begin() + 2 points to index 2.
  • The value 3 is inserted at that position.
  • Existing elements 4 and 5 are shifted one position to the right.
  • The final vector becomes {1, 2, 3, 4, 5}.

Complexity Analysis

  • Time Complexity: O(n) in the worst case because elements may need to be shifted.
  • Space Complexity: O(1) if reallocation is not required; otherwise additional memory may be allocated internally.

Insert Multiple Copies of an Element

The vector::insert() function can also be used to insert multiple copies of the same element at a specified position in a vector. All existing elements at and after the insertion position are shifted to accommodate the new elements.

Syntax

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.
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 

Explanation

  • v.begin() + 1 points to index 1 of the vector.
  • insert() inserts 3 copies of the value 2 before that position.
  • Existing elements (3, 4, and 5) are shifted to the right.
  • The resulting vector becomes {1, 2, 2, 2, 3, 4, 5}.

Complexity Analysis

  • Time Complexity: O(n) in the worst case due to shifting of elements.
  • Space Complexity: O(1) if no reallocation occurs; otherwise additional memory may be allocated internally.

Insert a List of Elements

The vector::insert() function can insert multiple elements from an initializer list at a specified position in the vector. The inserted elements maintain the same order as they appear in the initializer list.

Syntax

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.
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 

Explanation

  • v.begin() + 3 points to the end of the vector.
  • The elements 4 and 5 from the initializer list are inserted at that position.
  • The elements are inserted in the same order as specified in the list.
  • The resulting vector becomes {1, 2, 3, 4, 5}.

Complexity Analysis

  • Time Complexity: O(n + m), where n is the number of elements shifted and m is the number of elements inserted.
  • Space Complexity: O(1) if reallocation is not required; otherwise additional memory may be allocated internally.

Insert a Range of Elements

The vector::insert() function can also insert a range of elements from another container or array into a vector at a specified position. The elements are inserted in the same order as they appear in the source range.

Syntax

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.
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 

Explanation

  • l.begin() and l.end() define the range of elements to be inserted.
  • v.begin() + 3 points to the end of the vector.
  • All elements from the list (4 and 5) are copied into the vector.
  • The resulting vector becomes {1, 2, 3, 4, 5}.

Complexity Analysis

  • Time Complexity: O(n + m), where n is the number of elements shifted and m is the number of elements inserted.
  • Space Complexity: O(1) if no reallocation occurs; otherwise additional memory may be allocated internally.

vector::insert() Vs vector::push_back()

Both vector::insert() and vector::push_back() are used to add elements to a vector, but they differ in terms of insertion position, flexibility, and performance.

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)
Comment