A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/vector-insert-function-in-cpp-stl/ below:

Vector insert() in C++ STL

Vector insert() in C++ STL

Last Updated : 11 Jul, 2025

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;
}

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

Parameters

Return Value

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;
}

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

Parameters

Return Value

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;
}
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

Return Value

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;
}
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

Return Value

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;
}
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():

Feature Vector insert() Vector push_back() Position Inserts at any specified position. Always appends at the end of the vector. Number of Elements Can insert single or multiple elements or a range. Inserts only one element at a time. Element Shifting Shifts elements after the insertion point. No shifting of elements (added at the end). Time Complexity O(n) O(1)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4