C++ set insert
The C++ set insert function is utilized to add a new element into the set.
Since each key in a set is unique, the insertion process begins by verifying if the specified key is already existing within the set. If the key is found in the set, it will not be added again, and instead, the iterator pointing to the existing key will be returned. However, if the key is not present in the set, a new element containing the key will be inserted into the set.
Syntax
single element (1) pair<iterator,bool> insert (const value_type& val); //until C++ 11
with hint (2) iterator insert (iterator position, const value_type& val); //until C++ 11
range (3) template <class InputIterator>
void insert (InputIterator first, InputIterator last); //until C++ 11
single element (1) pair<iterator,bool> insert (const value_type& val);
pair<iterator,bool> insert (value_type&& val); //since C++ 11
with hint (2) iterator insert (const_iterator position, const value_type& val);
iterator insert (const_iterator position, value_type&& val); //since C++ 11
range (3) template <class InputIterator>
void insert (InputIterator first, InputIterator last); //since C++ 11
initializer list (4) void insert (initializer_list<value_type> il); //since C++ 11
Parameter
val : value to insert in the set.
position: Suggestion for the placement of the element within the set.
first : Beginning of range to insert.
last : End of range to insert.
il : An initializer list.
Return value
Returns a boolean pair to indicate whether an insertion occurred and provides an iterator pointing to the newly added element.
Complexity
- If a single element is inserted complexity will be logarithmic in size.
- If a hint is given and the position given is the optimal then the complexity will be amortized constant.
Iterator validity
No changes.
Data Races
The container is modified.
Exception Safety
This function does not throw exception.
Example 1
Let's explore a straightforward example of adding elements to a set:
#include <iostream>
#include <set>
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:
The elements in set are: 1 2 3 4 5
In the previous example, it merely adds the element with the specified key.
Example 2
Let's examine a basic example of inserting the element at the designated location:
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> s;
// Function to insert elements
// in the set container
auto itr = s.insert(s.begin(), 1);
// the time taken to insertion
// is very less as the correct
// position for insertion is given
itr = s.insert(itr, 4);
itr = s.insert(itr, 2);
itr = s.insert(itr, 5);
itr = s.insert(itr, 3);
cout << "The elements in set are: ";
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
return 0;
}
Output:
The elements in set are: 1 2 3 4 5
In the example provided, elements are added at the specified location.
Example 3
Let's explore a basic example demonstrating the process of adding elements from one set to another within a specified range:
#include <iostream>
# include<iostream>
# include<set>
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 3 is to end is
// 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:
The elements in set1 are: 1 2 3 4 5
The elements in set2 are: 3 4 5
Example 4
Let's explore a basic illustration of inserting elements from the initializer list:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
set<string> m = {"Java", "C++", "SQL"};
// Insert the elements from an initializer_list
m.insert({"VB", "Oracle"});
cout << "Set contains following elements" << endl;
for (auto it = m.begin(); it != m.end(); ++it)
cout << *it<< endl;
return 0;
}
Output:
Set contains following elements
C++
Java
Oracle
SQL
VB
In the previous example, elements are added from an initializer list.