C++ Standard Library C++ STL Library

C++ set - insert() Function



The C++ set::insert function is used to insert new elements in the container. This results into increasing the set size by the number of elements inserted. As the elements in a set are unique, therefore the insertion operation first checks if the inserted element is unique to the set then the element is inserted.

Syntax

//single element version  
pair<iterator,bool> insert (const value_type& val);

//single element with hint version  
iterator insert (iterator position, const value_type& val);

//range version 
template <class InputIterator>
  void insert (InputIterator first, InputIterator last);
//single element version 
pair<iterator,bool> insert (const value_type& val);
pair<iterator,bool> insert (value_type&& val);

//single element with hint version   
iterator insert (const_iterator position, const value_type& val);
iterator insert (const_iterator position, value_type&& val);

//range version 
template <class InputIterator>
  void insert (InputIterator first, InputIterator last);

//initializer list version 
void insert (initializer_list<value_type> il);

Parameters

position Specify the hint for the position where the element can be inserted.
val Specify the value to be copied (or moved) to the inserted elements.
first Specify the starting position of InputIterator. Copies of the elements in the range [first,last) are inserted in the container.
last Specify the last position of InputIterator. Copies of the elements in the range [first,last) are inserted in the container.
il Specify the initializer_list object.

Return Value

The single element version returns a pair, with pair::first set to an iterator pointing to either newly inserted element or to the equivalent element present in the set. The pair::second is set to true if new element is inserted in the set, false otherwise.

The single element with hint version returns an iterator pointing to either newly inserted element or to the equivalent element present in the set.

Time Complexity

Logarithmic i.e, Θ(log(n)) if a single element is inserted, but constant i.e, Θ(1) if position provided is optimal.

Example:

In the example below, the set::insert function is used to insert elements in the given set.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  set<int> set1 = {10, 20, 30};
  set<int> set2 = {10, 20, 30};
  set<int>::iterator it;

  //single element version 
  set1.insert(55);

  //single element with hint version 
  it = set2.begin();
  set2.insert(++it, 15);

  cout<<"set1 contains: ";
  for(it = set1.begin(); it != set1.end(); ++it)
    cout<<*it<<" ";

  cout<<"\nset2 contains: ";
  for(it = set2.begin(); it != set2.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

set1 contains: 10 20 30 55 
set2 contains: 10 15 20 30 

Example:

A range of elements can also be inserted into a set. Consider the example below.

#include <iostream>
#include <set>
#include <vector>
using namespace std;
 
int main (){
  set<int> MySet = {10, 20, 30};
  vector<int> MyVec = {15, 30, 45, 60, 75};
  set<int>::iterator set_it;
  vector<int>::iterator vec_it;

  //range version - insert a range of 
  //elements of MyVec into MySet
  vec_it = MyVec.begin();
  MySet.insert(vec_it, vec_it + 3);

  cout<<"MySet contains: ";
  for(set_it = MySet.begin(); set_it != MySet.end(); ++set_it)
    cout<<*set_it<<" ";

  return 0;
}

The output of the above code will be:

MySet contains: 10 15 20 30 45 

Example:

Similarly, the initializer list version can be used to insert elements in the given set.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  set<int> MySet = {10, 15};
  set<int>::iterator it;

  //initializer list version 
  initializer_list<int> ilist = {11, 12, 13, 14};
  MySet.insert(ilist); 

  cout<<"MySet contains: ";
  for(it = MySet.begin(); it != MySet.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

MySet contains: 10 11 12 13 14 15 

❮ C++ <set> Library