Insert Function in C++
Insert Function:
The insert function in C++ can be used to add elements to a variety of data structures, including vectors, strings, maps, sets, and more. Depending on the container it is used with, it behaves differently. In most cases, the insert method just requires one or two arguments, and it lets you add elements either at the end of the container or at a precise place.
- The elements are inserted into the vector using the C++ insert function.
- Either a specific place in the vector or the entire vector can be inserted to add elements.
- The insert function automatically reallocation memory when elements are added to a vector.
- The size grows or falls to its regular capacity if the total number of elements exceeds the overall capacity.
Vector insert() Function:
The built-in C++ STL function std::vector::insert() effectively increases the container size by the number of inserted elements by inserting new elements before the element at the provided position.
Time Complexity: O(N) Linear
The following cases are among those for which the insert function is overloaded:
- Add an element to the particular index.
- Add an element several times.
- Add a number of entries to the specified index.
1. Add a new element at the specified index:
Syntax of insert() in Vector:
vector_name.insert (position, val);
Parameters:
- Position – This defines the iterator that points to the desired position for insertion.
- Val - The value to be inserted is specified by the word "val."
Example of insert() function in vector:
// 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};
// Printing the original vector
cout << "Original vector :\n";
for (auto x : vector_name)
cout << x << " ";
cout << "\n";
// Inserting the value 50 at position 3(0-based
// indexing) in the vector
vector_name.insert(vector_name.begin() + 3, 50);
// Printing the modified vector
cout << "Vector after inserting 50 at position 3 :\n";
for (auto x : vector_name)
cout << x << " ";
cout << "\n";
// Inserting the value 60 at the position in the vector
vector_name.insert(vector_name.begin() + 1, 60);
// Printing the modified vector
cout << "Vector after inserting 60 at position 1 :\n";
for (auto x : vector_name)
cout << x << " ";
return 0;
}
Output:
![Insert Function in C++](https://static.tutorialandexample.com/cpp/insert-function-in-cpp1.png)
2. Add several elements to the specified index:
Syntax of insert() in Vector:
vector_name.insert(position, size, val)
Parameters
Position – This 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."
Example of insert() function in Vector:
// 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 << "Org 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:
![Insert Function in C++](https://static.tutorialandexample.com/cpp/insert-function-in-cpp2.png)
3. Insert the Range of Elements at Given Index:
Syntax of Vector insert()
vector_name.insert(position, iterator1, iterator2)
Parameters:
The following three parameters are accepted by the function:
Location - It indicates where in the vector the insertion is to be made.
Iterator 1: 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).
Example of Vector insert() function:
// 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};
vector<int> temp{ 2, 5, 9, 4, 3 };
// 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 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:
![Insert Function in C++](https://static.tutorialandexample.com/cpp/insert-function-in-cpp3.png)
4.Set insert() Function in STL:
The built-in C++ STL method set::insert puts elements into the set container or moves elements from one point in the set to another position in a different set.
The function accepts a necessary parameter element to be added to the set container as a parameter.
The function's return value is an iterator pointing to the element that was added to the container.
Log(N) for time complexity where 'N' denotes the set's number of elements
Syntax:
iterator set_name.insert(iterator position, element)
The function accepts the following two parameters, which are each described below:
- Element: The element to be added to the set container is specified.
- Location: It indicates a position from which the searching operation should be begun for insertion to speed up the process; it does not define the position where the insertion is to be done. The insertion is carried out in the same order as the predetermined container.
The program below exemplifies the action mentioned above:
// CPP program to demonstrate the
// set::insert(element) function
#include <bits/stdc++.h>
using namespace std;
int main()
{
set<int> s;
// Function to insert elements
//In the set container
s.insert(1);
s.insert(4);
s.insert(2);
s.insert(5);
s.insert(3);
cout << "The elements in set are: ";
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
return 0;
}
Output:
![Insert Function in C++](https://static.tutorialandexample.com/cpp/insert-function-in-cpp4.png)
void set insert Syntax:
void set_name.insert(iterator position1, iterator position2)
Two parameters, position1, and position2, which define the range of elements, are accepted by the function.
- [position1, last]: The range [position1, last]'s items are all added to another set container.
Return Type
Value Returned: No return type results in void.
The program below exemplifies the action above:
// CPP program to demonstrate the set::insert(iterator1, iterator2) function
#include <bits/stdc++.h>
using namespace std;
int main()
{
set<int> s1;
// Function to insert elements
//In the set container
s1.insert(1);
s1.insert(4);
s1.insert(2);
s1.insert(5);
s1.insert(3);
cout << "The elements in set1 are: ";
for (auto it = s1.begin(); it != s1.end(); it++)
cout << *it << " ";
set<int> s2;
// Function to insert one set to another
//All elements from where three is to end are
// inserted to set2
s2.insert(s1.find(3), s1.end());
cout << "\nThe elements in set2 are: ";
for (auto it = s2.begin(); it != s2.end(); it++)
cout << *it << " ";
return 0;
}
Output:
![Insert Function in C++](https://static.tutorialandexample.com/cpp/insert-function-in-cpp5.png)