C++ Standard Library C++ STL Library

C++ unordered_set - insert() Function



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

Syntax

//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 unordered_set to an iterator pointing to either newly inserted element or to the equivalent element present in the unordered_set. The pair::second is unordered_set to true if new element is inserted in the unordered_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 unordered_set.

Time Complexity

  • Single element insertion: Average case - constant i.e, Θ(1). Worst case - linear i.e, Θ(n).
  • Multiple elements insertion: Average case - linear in number of elements inserted. Worst case - quadratic: number of elements inserted multiplied by (container size + 1).

Example:

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

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  unordered_set<int> uSet1 = {10, 20, 30};
  unordered_set<int> uSet2 = {10, 20, 30};
  unordered_set<int>::iterator it;

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

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

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

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

  return 0;
}

The output of the above code will be:

uSet1 contains: 10 55 20 30 
uSet2 contains: 15 10 20 30 

Example:

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

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

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

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

  return 0;
}

The output of the above code will be:

uSet contains: 15 45 10 20 30 

Example:

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

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

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

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

  return 0;
}

The output of the above code will be:

uSet contains: 14 13 12 11 10 15 

❮ C++ <unordered_set> Library