vector insert() function in C++ STL - GeeksforGeeks

23 Jan, 2020
std::vector::insert() is a built-in function in C++ STL which inserts new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted.
Syntax:vector_name.insert (position, val)
Parameter:The function accepts two parameters specified as 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.Return value: The function returns an iterator which points to the newly inserted element.Example 1: Below program illustrates the above mentioned function where new elements are inserted at the front.// Program below illustrates the// vector::insert() function  #include <bits/stdc++.h>using namespace std;  int main(){    // initialising the vector    vector<int> vec = { 10, 20, 30, 40 };      // inserts 3 at front    auto it = vec.insert(vec.begin(), 3);    // inserts 2 at front    vec.insert(it, 2);      int i = 2;    // inserts 7 at i-th index    it = vec.insert(vec.begin() + i, 7);      cout << "The vector elements are: ";    for (auto it = vec.begin(); it != vec.end(); ++it)        cout << *it << " ";      return 0;}Output:The vector elements are: 2 3 7 10 20 30 40
Example 2: Below program illustrates the above mentioned function where new elements are inserted at a specific position.// Program below illustrates the// vector::insert() function  #include <bits/stdc++.h>using namespace std;  int main(){    // initialising the vector    vector<int> vec = { 10, 20, 70, 80 };    int x = 50;      // inserting multiple elements    // at specific positions    vec.insert(vec.begin() + 2, { 30, 40, x, 60 });      cout << "The vector elements are: ";    for (auto it : vec)        cout << it << " ";      return 0;}Output:The vector elements are: 10 20 30 40 50 60 70 80
Syntax:vector_name.insert(position, size, val)
Parameter: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.Return value: The function returns an iterator which points to the newly inserted element.Below program illustrates the above mentioned function:// program below illustrates the// vector::insert() function  #include <bits/stdc++.h>using namespace std;  int main(){    // initialising the vector    vector<int> vec = { 10, 20, 30, 40 };      // inserts 3 one time at front    auto it = vec.insert(vec.begin(), 1, 3);      // inserts 4 two times at front    vec.insert(it, 2, 4);      cout << "The vector elements are: ";    for (auto it = vec.begin(); it != vec.end(); ++it)        cout << *it << " ";      return 0;}Output:The vector elements are: 4 4 3 10 20 30 40
Syntax:vector_name.insert(position, iterator1, iterator2)
Parameter:The function accepts three parameters specified as below:position – It specifies the position at which insertion is to be done in vector.iterator1 – It specifies the starting position from which the elements are to be insertediterator2 – It specifies the ending position till which elements are to be insertedReturn value: The function returns an iterator which points to the newly inserted element.Below is the illustration of above function:// program below illustrates the// vector::insert() function  #include <bits/stdc++.h>using namespace std;  int main(){    // initialising the vector    vector<int> vec1 = { 10, 20, 30, 40 };    vector<int> vec2;      // inserts at the beginning of vec2    vec2.insert(vec2.begin(), vec1.begin(), vec1.end());      cout << "The vector2 elements are: ";    for (auto it = vec2.begin(); it != vec2.end(); ++it)        cout << *it << " ";      return 0;}Output:The vector2 elements are: 10 20 30 40

Syntax:vector_name.insert (position, val)
Parameter:The function accepts two parameters specified as 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.Return value: The function returns an iterator which points to the newly inserted element.Example 1: Below program illustrates the above mentioned function where new elements are inserted at the front.// Program below illustrates the// vector::insert() function  #include <bits/stdc++.h>using namespace std;  int main(){    // initialising the vector    vector<int> vec = { 10, 20, 30, 40 };      // inserts 3 at front    auto it = vec.insert(vec.begin(), 3);    // inserts 2 at front    vec.insert(it, 2);      int i = 2;    // inserts 7 at i-th index    it = vec.insert(vec.begin() + i, 7);      cout << "The vector elements are: ";    for (auto it = vec.begin(); it != vec.end(); ++it)        cout << *it << " ";      return 0;}Output:The vector elements are: 2 3 7 10 20 30 40
Example 2: Below program illustrates the above mentioned function where new elements are inserted at a specific position.// Program below illustrates the// vector::insert() function  #include <bits/stdc++.h>using namespace std;  int main(){    // initialising the vector    vector<int> vec = { 10, 20, 70, 80 };    int x = 50;      // inserting multiple elements    // at specific positions    vec.insert(vec.begin() + 2, { 30, 40, x, 60 });      cout << "The vector elements are: ";    for (auto it : vec)        cout << it << " ";      return 0;}Output:The vector elements are: 10 20 30 40 50 60 70 80

vector_name.insert (position, val)

Parameter:The function accepts two parameters specified as 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.
Return value: The function returns an iterator which points to the newly inserted element.
Example 1: Below program illustrates the above mentioned function where new elements are inserted at the front.
// Program below illustrates the// vector::insert() function  #include <bits/stdc++.h>using namespace std;  int main(){    // initialising the vector    vector<int> vec = { 10, 20, 30, 40 };      // inserts 3 at front    auto it = vec.insert(vec.begin(), 3);    // inserts 2 at front    vec.insert(it, 2);      int i = 2;    // inserts 7 at i-th index    it = vec.insert(vec.begin() + i, 7);      cout << "The vector elements are: ";    for (auto it = vec.begin(); it != vec.end(); ++it)        cout << *it << " ";      return 0;}
The vector elements are: 2 3 7 10 20 30 40

Example 2: Below program illustrates the above mentioned function where new elements are inserted at a specific position.
// Program below illustrates the// vector::insert() function  #include <bits/stdc++.h>using namespace std;  int main(){    // initialising the vector    vector<int> vec = { 10, 20, 70, 80 };    int x = 50;      // inserting multiple elements    // at specific positions    vec.insert(vec.begin() + 2, { 30, 40, x, 60 });      cout << "The vector elements are: ";    for (auto it : vec)        cout << it << " ";      return 0;}
The vector elements are: 10 20 30 40 50 60 70 80

Syntax:vector_name.insert(position, size, val)
Parameter: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.Return value: The function returns an iterator which points to the newly inserted element.Below program illustrates the above mentioned function:// program below illustrates the// vector::insert() function  #include <bits/stdc++.h>using namespace std;  int main(){    // initialising the vector    vector<int> vec = { 10, 20, 30, 40 };      // inserts 3 one time at front    auto it = vec.insert(vec.begin(), 1, 3);      // inserts 4 two times at front    vec.insert(it, 2, 4);      cout << "The vector elements are: ";    for (auto it = vec.begin(); it != vec.end(); ++it)        cout << *it << " ";      return 0;}Output:The vector elements are: 4 4 3 10 20 30 40

vector_name.insert(position, size, val)

Parameter: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.
Return value: The function returns an iterator which points to the newly inserted element.
Below program illustrates the above mentioned function:
// program below illustrates the// vector::insert() function  #include <bits/stdc++.h>using namespace std;  int main(){    // initialising the vector    vector<int> vec = { 10, 20, 30, 40 };      // inserts 3 one time at front    auto it = vec.insert(vec.begin(), 1, 3);      // inserts 4 two times at front    vec.insert(it, 2, 4);      cout << "The vector elements are: ";    for (auto it = vec.begin(); it != vec.end(); ++it)        cout << *it << " ";      return 0;}
The vector elements are: 4 4 3 10 20 30 40

Syntax:vector_name.insert(position, iterator1, iterator2)
Parameter:The function accepts three parameters specified as below:position – It specifies the position at which insertion is to be done in vector.iterator1 – It specifies the starting position from which the elements are to be insertediterator2 – It specifies the ending position till which elements are to be insertedReturn value: The function returns an iterator which points to the newly inserted element.Below is the illustration of above function:// program below illustrates the// vector::insert() function  #include <bits/stdc++.h>using namespace std;  int main(){    // initialising the vector    vector<int> vec1 = { 10, 20, 30, 40 };    vector<int> vec2;      // inserts at the beginning of vec2    vec2.insert(vec2.begin(), vec1.begin(), vec1.end());      cout << "The vector2 elements are: ";    for (auto it = vec2.begin(); it != vec2.end(); ++it)        cout << *it << " ";      return 0;}Output:The vector2 elements are: 10 20 30 40

vector_name.insert(position, iterator1, iterator2)

Parameter:The function accepts three parameters specified as below:
position – It specifies the position at which insertion is to be done in 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
Return value: The function returns an iterator which points to the newly inserted element.
Below is the illustration of above function:
// program below illustrates the// vector::insert() function  #include <bits/stdc++.h>using namespace std;  int main(){    // initialising the vector    vector<int> vec1 = { 10, 20, 30, 40 };    vector<int> vec2;      // inserts at the beginning of vec2    vec2.insert(vec2.begin(), vec1.begin(), vec1.end());      cout << "The vector2 elements are: ";    for (auto it = vec2.begin(); it != vec2.end(); ++it)        cout << *it << " ";      return 0;}
The vector2 elements are: 10 20 30 40

grim_firefly
CPP-Functions
cpp-vector
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in C++
C++ Classes and Objects
Socket Programming in C/C++
Operator Overloading in C++
Copy Constructor in C++
Virtual Function in C++
Templates in C++ with Examples
rand() and srand() in C/C++
vector erase() and clear() in C++
unordered_map in C++ STL