Updated March 29, 2023
Introduction to C++ Insert
As we already know, a vector is just like a dynamic array used to store the elements, but it has the special ability to resize according to the elements inserted or deleted. Insert function in c++ is used to insert the elements in the vector. Insertion of elements can be at a particular position or insertion of the whole vector into the other vector. The insertion of elements in a vector using the insert function allows the automatic reallocation of memory. If the total number of elements extends the overall capacity, it increases/ decreases the size to its normal capacity.
Syntax
Below given is the basic syntax of the Insert function in C++ for inserting elements normally:
iterator s_name.insert(element)
Syntax to insert the elements using insert function in C++ at the desired position:
iterator s_name.insert(position, element)
Syntax to insert multiple elements or a range of elements at the desired position:
iterator s_name.insert(position, iterator_first, iterator_last)
where,
s_name: It is the set name on which the insertion of elements will take place.
element: It is a mandatory parameter that contains the element or the list of elements that needs to be inserted in the given set.
position: It defines the position count where the element needs to be inserted in the set.
Return value: On using the insert function, ‘iterator’ is returned, pointing to the first element of the inserted values.
first, last: It defines the range of elements to be inserted between the first and last, including the ‘first’ element but not the one pointing to ‘last.’
How does the Insert function work in C++?
As we have already told above, the insert() function in C++ inserts the elements in the vector. It works according to the parameter values provided in the arguments of how the insertion will take place, either normally or at a particular position. Let’s understand the working of insert() function in different ways with the examples given below:
Example #1
Insertion of a single value in Vector in C++
Code:
// insertion of a single value in the vector
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> vec1 {100, 90, 80, 70};
cout<<" Vector values in the starting are: ";
for(auto x=vec1.begin(); x<vec1.end(); x++) //for loop to traverse all the vector 'vec1' elements
{
cout<<" / "<<*x; //printing the values on console
}
vec1.insert(vec1.end(),60); //Inserting element '60' to the vector 'vec1' at the end
cout<<"\nVector values after modification are: ";
for(auto x=vec1.begin(); x<vec1.end(); x++) //for loop to traverse all the vector elements
{
cout<<" / "<<*x;
}
return 0;
}
Output:
Explanation:
In the above example, we are initializing the elements of vector ‘vec1’. In order to insert the elements insert(vec1.end(), element) is used. In this function, vec1.end() is used to start inserting the elements at the last of the vec1. The second parameter is the element to be inserted. In the end, the final vector with the new values inserted is printed on the console.
Example #2
Insertion of multiple values or a vector array in another vector
Code:
// insertion of single value in vector at a particular position
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> vec1 {100, 90, 80, 70};
cout<<" Vector values in the starting are: ";
for(auto x=vec1.begin(); x<vec1.end(); x++) // for loop to traverse all the vector 'vec1' elements
{
cout<<" / "<<*x; // printing the values on console
}
auto pos =vec1.begin() + 2; // defining the position of vector
vec1.insert(pos,50); //Inserting element '50' to the vector 'vec1' at the 2nd position
cout<<"\nVector values after modification are: ";
for(auto x=vec1.begin(); x<vec1.end(); x++) // for loop to traverse all the vector elements
{
cout<<" / "<<*x;
}
return 0;
}
Output:
Explanation:
In the above example, we are initializing the value of vector ‘vec1’. To differentiate, we are first printing the original elements of vec1.To insert the element at a particular position, a variable ‘pos’ is used to define the position, and then the insert function is used to insert at the position ‘pos.’ Parameters (pos, element) are used to define the position and the element to be inserted at that position.
Example #3
Insertion of elements at a particular position in a vector
Code:
// insertion of single value in vector
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> vec1 {100, 90,80, 70};
vector<int> vec2 {10, 9, 8, 7};
cout<<" Vector values in the vec1 are: ";
for(auto x=vec1.begin(); x<vec1.end(); x++) // for loop to traverse all the vector 'vec1' elements
{
cout<<" / "<<*x; // printing the values on console
}
cout<<" \nVector values in the vec2 are: ";
for(auto x=vec2.begin(); x<vec2.end(); x++) // for loop to traverse all the vector 'vec2' elements
{
cout<<" / "<<*x; // printing the values on console
}
// Inserting the vec1 values in vec2 in the end
vec2.insert(vec2.end(), vec1.begin(), vec1.end()); //Inserting elements of vector 'vec1' into the vector 'vec2' at the end
cout<<"\nVector values after modification are: ";
for(auto x=vec2.begin(); x<vec2.end(); x++) // for loop to traverse all the vector elements
{
cout<<" / "<<*x;
}
return 0;
}
Output:
Explanation:
In the above example, we have declared the 2 vectors, ‘vec1’ and ‘vec2’. First, we are printing the original values of elements of both the vectors. Then, the insert () function is used to insert the elements of vec1 into vec2. It takes the parameter (vec2.end(), vec1.begin(), vec1.end()), which means that the insertion should start from the ending of vector ‘vec2’ and takes the range from the starting of vec1 till the end.
Conclusion
The above description clearly explains what is an insert function in C++ and how it is used while programming in C++. Apart from the one explained above, a string insert function is used to insert a character at a particular position before or after. It has the syntax as str.insert(position, str_to_insert). insert() is one of the most important functions and should be understood thoroughly by the programmer before using it.
Recommended Articles
This is a guide to C++ Insert. Here we discuss How Insert function works in C++ and Examples along with codes and outputs. You may also have a look at the following articles to learn more –