Unordered Sets in C++

In this tutorial, we will understand in detail about the Unordered Sets in C++. In this, we would first discuss the Unordered set in C++ and followed by we will understand it in detail with an example.

The Unordered Set is basically an associative container in the STL. The Unordered Set is similar to the Set concept in C++. But the Unordered Set stores the keys in a random or unordered manner. The key which we define in the container can of any type predefined or it can be user definer. Also, the Unordered Set uses Hashtable in order to store all the keys inside the container. Hence, it’s best time complexity in order to search, insert or delete is O(1) to the O(n).

Now, let us understand the Unordered Set with an example. Where in which we will be using the predefined methods.

//illustraiting the unordered_set in C++
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
  // declaring the unordered_set of type int
  unordered_set<int> cs ; 

  //inserting the int values  

  cs.insert(10); 
  cs.insert(20); 
  cs.insert(30); 
  cs.insert(40); 
  cs.insert(50); 
  
  //elements to findout using find() function 
  //from the container
  
  int int_f1 = 40;
  int int_f2 = 60;

  // the find() function returns either the contant iterator or 
  //iterator if key is found

  if (cs.find(int_f1) == cs.end()) 
    cout << "Key "<<int_f1 << " not present in the container\n\n"; 
  else
    cout << "Key " << int_f1 <<"present in the container\n\n"; 

  if (cs.find(int_f2) == cs.end()) 
    cout << "Key "<<int_f2 << " not present in the container\n\n"; 
  else
    cout << "Key " << int_f2 <<"present in the container\n\n"; 
  

  // traversing and printing all the keys from the container
  
  cout << "\nAll keys in the container : "; 
  unordered_set< int > :: iterator it; 
  for (it = cs.begin(); it != cs.end(); it++) 
    cout << (*it) << endl; 
} 

The above program gives the output as,

Key 40present in the container                                                                                                               
                                                                                                                                             
Key 60 not present in the container                                                                                                          
                                                                                                                                             
                                                                                                                                             
All keys in the container: 50                                                                                                               
40      
30                                                                                                                                           
10                                                                                                                                           
20

 

Also read:

unordered_set find() function in C++

Print all possible subsets of a set in C++

Leave a Reply

Your email address will not be published. Required fields are marked *