vector insert() Function in C++ STL
Last Updated :
06 May, 2023
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:
- Insert an element at the given index.
- Insert an element multiple times.
- 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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< int > vector_name{ 1, 2, 3, 4, 5 };
cout << "Original vector :\n" ;
for ( auto x : vector_name)
cout << x << " " ;
cout << "\n" ;
vector_name.insert(vector_name.begin() + 3, 100);
cout << "Vector after inserting 100 at position 3 :\n" ;
for ( auto x : vector_name)
cout << x << " " ;
cout << "\n" ;
vector_name.insert(vector_name.begin() + 1, 500);
cout << "Vector after inserting 500 at position 1 :\n" ;
for ( auto x : vector_name)
cout << x << " " ;
return 0;
}
|
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< int > vector_name{ 1, 2, 3, 4, 5 };
cout << "Original vector :\n" ;
for ( auto x : vector_name)
cout << x << " " ;
cout << endl;
vector_name.insert(vector_name.begin() + 3, 4, 100);
cout << "Vector after inserting 100, 4 times, starting "
"at position 3 :\n" ;
for ( auto x : vector_name)
cout << x << " " ;
return 0;
}
|
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< int > original{ 1, 2, 3, 4, 5 };
vector< int > temp{ 2, 5, 9, 0, 3, 10 };
cout << "Original vector :\n" ;
for ( auto x : original)
cout << x << " " ;
cout << endl;
original.insert(original.begin() + 3, temp.begin() + 2,
temp.begin() + 5);
cout << "Vector after Inserting the portion of temp "
"vector in original vector :\n" ;
for ( auto x : original)
cout << x << " " ;
return 0;
}
|
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...