The Wayback Machine - https://web.archive.org/web/20240717024422/https://www.geeksforgeeks.org/vector-insert-function-in-cpp-stl/
Open In App

vector insert() Function in C++ STL

Last Updated : 06 May, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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)

The insert function is overloaded to work on multiple cases which are as follows:

  1. Insert an element at the given index.
  2. Insert an element multiple times.
  3. Insert a range of elements at the given index.

1. Insert an Element at the Given Index

Syntax of insert() in Vector

vector_name.insert (position, val);

Parameters

The function accepts two parameters specified 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.

Example of insert() in vector

C++




// C++ 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. Insert Multiple Elements at Given Index 

Syntax of insert() in Vector

vector_name.insert(position, size, val)

Parameters

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.

Example of insert() in Vector

C++




// C++ 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. Insert the Range of Elements at Given Index

Syntax of Vector insert()

vector_name.insert(position, iterator1, iterator2)

Parameters

The function accepts three parameters specified 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

Example of Vector insert()

C++




// C++ 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 starting 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 


Previous Article
Next Article

Similar Reads

vector::front() and vector::back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::front() This function can be used to fetch the first element of a vector container.Syntax : vectorname.front() Parameters : No value is needed to pass as
3 min read
vector::empty() and vector::size() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::empty() The empty() function is used to check if the vector container is empty or not.Syntax : vectorname.empty() Parameters : No parameters are p
5 min read
vector::push_back() and vector::pop_back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::push_back() push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the cur
4 min read
vector::at() and vector::swap() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::at() at() function is used reference the element present at the position given as the parameter to the function. Syntax: vectorname.at(position) Parameter
4 min read
vector::begin() and vector::end() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::begin() begin() function is used to return an iterator pointing to the first element of the vector container. begin() function returns a bidirectional
3 min read
vector :: cbegin() and vector :: cend() in C++ STL
Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container. vector::cbegin() The function returns an iterator which is used to iterate container. The iterator points to the beginning of the vector.Iterator cannot modify the contents of the vector. Synta
2 min read
Initializing Vector using an Existing Vector in C++ STL
A vector is a type of container which can store objects of similar data type. Vector acts like a dynamic array where we can insert elements and the size of the array increases depending upon the elements inserted. Syntax: vector&lt;data_structure/type&gt; vector_name(size, item) To know more about vectors refer to vectors in C++. The 3D vector in C
5 min read
vector::operator= and vector::operator[ ] in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::operator=This operator is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new
4 min read
multiset insert() function in C++ STL
The multiset::insert() is a built-in function in C++ STL which insert elements in the multiset container or inserts the elements from a position to another position from one multiset to a different multiset. Syntax: iterator multiset_name.insert(element)Time Complexity: O(log n) Since the elements are always in sorted order, the newly inserted elem
4 min read
deque insert() function in C++ STL
The deque::insert() function is a built-in function in C++ which is used to insert elements in the deque. The insert() function can be used in three ways: Extends deque by inserting a new element val at a position.Extends deque by inserting n new element of value val in the deque.Extends deque by inserting new element in the range [first, last). Sy
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg