Vector insert() Function in C++
In this article, we will discuss the vector insert() function in C++ with syntax and examples.
Insert() Function:
The insert() Function in the C++ std::vector container allows you to insert components into the vector at a specific location. Depending on how you wish to use it, the insert() method is available in a few distinct formats. The insert() function can be used in the following ways frequently:
Time Complexity:
The time complexity of the vector insert() function is O(N) Linear.
The following cases are among those for which the insert function is overloaded:
- Add an element to the specified index.
- Add an element several times.
- Add a number of entries to the specified index.
1. Insert an Element at the given Index:
Use the insert() in a vector named vector_name to add an element at the specified index.
Syntax:
It has the following syntax:
vector_name.insert (position, val);
Parameters:
The following two parameters are accepted by the Function:
- Position – It defines the iterator that points to the desired position for insertion.
- Val - The value to be inserted is specified by the word "val".
Program to illustrate the Function:
// 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;
}
Output:
![Vector insert() Function in C++](https://static.tutorialandexample.com/cpp/vector-insert-function-in-cpp1.png)
2. Insert Multiple Elements at the Given Index
It adds several elements to the specified index.
Syntax:
It has the following syntax:
vector_name.insert(position, size, val)
Parameters:
The following three parameters are accepted by the Function:
- Position – It defines the iterator that points to the desired position for insertion.
- Size – It indicates how many times a value should be placed at the given location.
- Val - The value to be inserted is specified by the word "val".
Program to illustrate the Function:
// 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;
}
Output:
![Vector insert() Function in C++](https://static.tutorialandexample.com/cpp/vector-insert-function-in-cpp2.png)
3. Insert the Range of Elements at the Given Index:
It adds the range of elements at the specified index.
Syntax:
It had the following syntax:
vector_name.insert(position, iterator1, iterator2)
Parameters:
The following three parameters are accepted by the Function:
- Location - It indicates where the insertion is to be made in the vector.
- Iterator1- The starting location from which the elements are to be added is specified by iterator1.
- Iterator 2 - It defines the last location at which elements are to be put (iterator 2).
Program:
// 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 the temp vector in the 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;
}
Output:
![Vector insert() Function in C++](https://static.tutorialandexample.com/cpp/vector-insert-function-in-cpp3.png)
Example:
Certainly! Here is a sample program that shows how to utilize the insert() Function in various ways:
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
// Inserting a single element
std::vector<int>::iterator it = numbers.begin() + 2;
numbers.insert(it, 99); // Inserts 99 before the element at position 2
// Inserting a range of elements
std::vector<int> newNumbers = {77, 88};
it = numbers.begin() + 4;
numbers.insert(it, numbers.begin(), numbers.end());
// Inserting multiple copies of an element
it = numbers.begin() + 1;
numbers. insert(it, 3, 42); // Inserts three copies of 42 before the element at position 1
// Displaying the updated vector
std::cout << "Updated vector: ";
for (int num: numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
Output:
![Vector insert() Function in C++](https://static.tutorialandexample.com/cpp/vector-insert-function-in-cpp4.png)
Explanation:
- In this illustration, a vector of numbers from 1 to 5 is the starting point. The three versions of the insert() Function are shown in practice.
- The output is 1 42 99 42 42 with 99 inserted before the element at position 2, followed by 3 77 88 and 5.
- The result is 1 42 99 42 77 88 42 2 3 77 88 4 5. The elements 77 and 88 from the numbers vector are inserted before the element at position 4.
- Before the element in position 1, we add three copies of 42, creating the following numbers: 1 42 42 42 99 42 77 88 42 2 3 77 88 4 5.
- After that, the modified vector is shown in the program to confirm the modifications.