C++ Standard Library C++ STL Library

C++ unordered_set - unordered_set() Function



The C++ unordered_set::unordered_set function is used to construct a unordered_set object, initializing its contents depending on the version of constructor used:

Syntax

//default version - construct an empty 
//container with no elements
explicit unordered_set ( size_type n = /* see below */,
                         const hasher& hf = hasher(),
                         const key_equal& eql = key_equal(),
                         const allocator_type& alloc = allocator_type() );
explicit unordered_set ( const allocator_type& alloc );

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
         unordered_set ( InputIterator first, InputIterator last,
                         size_type n = /* see below */,
                         const hasher& hf = hasher(),
                         const key_equal& eql = key_equal(),
                         const allocator_type& alloc = allocator_type() );

//copy version - copies all elements
//of ust into the container	
unordered_set ( const unordered_set& ust );
unordered_set ( const unordered_set& ust, const allocator_type& alloc );

//move version - moves elements of ust
//into the container	
unordered_set ( unordered_set&& ust );
unordered_set ( unordered_set&& ust, const allocator_type& alloc );

//initializer list version - copies all 
//elements of il into the container	
unordered_set (initializer_list<value_type> il,
                size_type n = /* see below */,
                const hasher& hf = hasher(),
                const key_equal& eql = key_equal(),
                const allocator_type& alloc = allocator_type() );
//default version - construct an empty 
//container with no elements
unordered_set();
explicit unordered_set ( size_type n,
                         const hasher& hf = hasher(),
                         const key_equal& eql = key_equal(),
                         const allocator_type& alloc = allocator_type() );
explicit unordered_set ( const allocator_type& alloc );
         unordered_set ( size_type n, const allocator_type& alloc );
         unordered_set ( size_type n, const hasher& hf, const allocator_type& alloc );

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  unordered_set ( InputIterator first, InputIterator last,
                  size_type n = /* see below */,
                  const hasher& hf = hasher(),
                  const key_equal& eql = key_equal(),
                  const allocator_type& alloc = allocator_type() );
template <class InputIterator>
  unordered_set ( InputIterator first, InputIterator last, size_type n,
                  const allocator_type& alloc );
template <class InputIterator>
  unordered_set ( InputIterator first, InputIterator last, size_type n,
                  const hasher& hf, const allocator_type& alloc );

//copy version - copies all elements
//of ust into the container	
unordered_set ( const unordered_set& ust );
unordered_set ( const unordered_set& ust, const allocator_type& alloc );

//move version - moves elements of ust
//into the container	
unordered_set ( unordered_set&& ust );
unordered_set ( unordered_set&& ust, const allocator_type& alloc );

//initializer list version - copies all 
//elements of il into the container		
unordered_set (initializer_list<value_type> il,
                size_type n = /* see below */,
                const hasher& hf = hasher(),
                const key_equal& eql = key_equal(),
                const allocator_type& alloc = allocator_type() );
unordered_set ( initializer_list<value_type> il, size_type n,
                const allocator_type& alloc );
unordered_set ( initializer_list<value_type> il, size_type n,
                const hasher& hf, const allocator_type& alloc );

Parameters

alloc Specify the Allocator object. The container keeps and uses an internal copy of this allocator.
n It contains information about minimum number of initial buckets.
hf It is a hasher function object.
eql The comparison function object, that returns true if the two container object keys passed as arguments are equal.
first Specify initial position of the input iterator of the range. The range used is [first,last).
last Specify final position of the input iterator of the range. The range used is [first,last).
ust Specify an unordered_set object of same type.
il Specify an initializer_list object.

Return Value

Constructor never returns value.

Time Complexity

Constant i.e, Θ(1), for default version and move version.
For all the other cases, Average case: Linear i.e, Θ(n), Worst case: Quadratic i.e, Θ(n2).

Example: using default version

In the example below, the unordered_set::unordered_set function is used to construct an unordered_set object.

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  //default version - construct an empty unordered_set
  unordered_set<int> USet;
  unordered_set<int>::iterator it;

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

  //insert elements in USet
  USet.insert(10);
  USet.insert(20);

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

  return 0;
}

The output of the above code will be:

USet contains: 
USet contains: 20 10 

Example: using range and copy version

An unordered_set can also be constructed using range or copy version. Consider the following example:

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  unordered_set<int> USet1{100, 200, 300};
  unordered_set<int>::iterator it;

  //range version - construct USet2 using range
  unordered_set<int> USet2(USet1.begin(), USet1.end());

  //copy version - construct USet3 from USet1
  unordered_set<int> USet3(USet1); 

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

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

  return 0;
}

The output of the above code will be:

USet2 contains: 100 200 300 
USet3 contains: 300 200 100 

Example: using move version

Using the move version of unordered_set, the content of one unordered_set can be moved to another unordered_set. Consider the following example:

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  unordered_set<int> USet1{10, 20, 30, 40, 50};
  unordered_set<int>::iterator it;

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

  //moving all content of USet1 into USet2
  unordered_set<int> USet2(move(USet1));

  cout<<"\nUSet1 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: 50 40 30 20 10 
USet1 contains: 
USet2 contains: 50 40 30 20 10 

Example: using initializer list version

The initializer list can also be used to assign values into an unordered_set container. Consider the example below:

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  //creating initializer list
  initializer_list<int> ilist = {15, 30, 45, 60, 75};

  //initializer list version - copies all 
  //elements of ilist into the container	
  unordered_set<int> USet(ilist);
  unordered_set<int>::iterator it;

  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: 75 60 45 30 15 

❮ C++ <unordered_set> Library