CPP STL Tutorial : C++ std::unordered_set and it’s operations.

In this chapter we shall learn about:

1. unordered_set introduction.
2. unordered_set declaration.
3. unordered_set member function to get the Capacity.
4. unordered_set member function to iterate over elements
5. unordered_set member function for element lookup
6. unordered_set member function to get the buckets
7. unordered_set member function for modifying elements
8. unordered_set member function for Hash policy

1. unordered_set introduction.

Unordered sets stores unique elements in no particular order. Thus it will allow for fast retrieval of individual elements based on their value.

The value of an element is at the same time its key, that identifies it uniquely.

keys are immutable, hence they cannot be modified but they can be inserted and removed.

Internally, they are organized into buckets depending on their hash values to allow for fast access to individual elements directly by their values.

Below header file to be included while using unordered_set:

#include<unordered_set>

2. unordered_set declaration.

std::unordered_set<std::string> myset = {"Red","Blue","Green","Yellow","Black","White","Grey","Brown", "Black", "Yellow"};

3. unordered_set member function to get the Capacity.

Below are the member functions provided to get unordered_Set:

empty : It will test if container is empty
size : It will return container size
max_size : It will return the maximum size

#include <iostream>
#include <unordered_set>
#include <algorithm>    

//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main ()
{
  std::unordered_set<std::string> myset;
  if(myset.empty())
  {
    std::cout << "myset is empty. Size is = " << myset.size() << std::endl;
  }
  

  myset = {"Green","Yellow","Black"};
  std::cout << "After insert size: = " << myset.size() << std::endl;

  myset.insert ("White");
  std::cout << "After one more insert size: = " << myset.size() << std::endl;

  myset.erase ("White");
  std::cout << "After erase size: = " << myset.size() << std::endl;

  return 0;
}

Output:

myset is empty. Size is = 0
After insert size: = 3
After one more insert size: = 4
After erase size: = 3

4. unordered_set member function to iterate over elements

Below are the member functions to iterate over the elements:

begin : It will return iterator to beginning
end : It will return iterator to end
cbegin : It will return const_iterator to beginning
cend : It will return const_iterator to end

#include <iostream>
#include <unordered_set>
#include <algorithm>    

//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main ()
{

  std::unordered_set<std::string> myset = {"Red","Blue","Green","Yellow","Black","White","Grey","Brown", "Black", "Yellow"};

  cout<<"Printing by using begin() and end()"<<endl;
  for(auto it = myset.begin(); it != myset.end(); it++)
  {
      cout<<*it<<endl;
  }
 
  cout<<"\nPrinting by using cbegin() and cend()"<<endl;
  for(auto it = myset.cbegin(); it != myset.cend(); it++)
  {
      cout<<*it<<endl;
  }
  return 0;
}

Output:

Printing by using begin() and end()
Grey
Black
Yellow
Green
Brown
Blue
White
Red

Printing by using cbegin() and cend()
Grey
Black
Yellow
Green
Brown
Blue
White
Red

5. unordered_set member function for element lookup

Below are the member functions for element lookup:

find : It will get iterator to element
count : It will count elements with a specific key
equal_range : It will get range of elements with a specific key

#include <iostream>
#include <unordered_set>
#include <algorithm>    

//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main ()
{

  std::unordered_set<std::string> myset = {"Red","Blue","Green","Yellow","Black","White","Grey","Brown", "Black", "Yellow"};

  cout<<"Below shows the usage of count()"<<endl;
  for (auto& x: {"Yellow","violet","White","apple"}) 
  {
    if (myset.count(x)>0)
      std::cout << "myset has " << x << std::endl;
    else
      std::cout << "myset has no " << x << std::endl;
  }
  
  cout<<"\nDemonstrate use of find()"<<endl;
  
  std::unordered_set<std::string>::const_iterator got = myset.find ("Green");

  if ( got == myset.end() )
    std::cout << "not found in myset";
  else
    std::cout << *got << " is in myset";
    
  return 0;
}

Output:

Input

Below shows the usage of count()
myset has Yellow
myset has no violet
myset has White
myset has no apple

Demonstrate use of find()
Green is in myset

6. unordered_set member function to get the buckets

Below are the member functions for buckets lookup:

bucket_count : It will return number of buckets
max_bucket_count : It will return maximum number of buckets
bucket_size : It will return bucket size
bucket : Locate element’s bucket

#include <iostream>
#include <unordered_set>
#include <algorithm>    

//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main ()
{
  std::unordered_set<std::string> myset = {"Red","Blue","Green","Yellow","Black","White","Grey","Brown", "Black", "Yellow"};
  
  unsigned n = myset.bucket_count();

  std::cout << "myset has " << n << " buckets.\n";

  for (unsigned i=0; i<n; ++i) 
  {
    std::cout << "bucket #" << i << " size is " << myset.bucket_size(i)<< " . It contains: ";
    for (auto it = myset.begin(i); it!=myset.end(i); ++it)
      std::cout << " " << *it;
    std::cout << "\n";
  }
  
  cout<<"\nGetting element bucket:"<<endl;
  //get the element bucket
  for (const std::string& x: myset) 
  {
    std::cout << x << " is in bucket #" << myset.bucket(x) << std::endl;
  }

  return 0;
}

Output:

myset has 11 buckets.
bucket #0 size is 2 . It contains: White Red
bucket #1 size is 0 . It contains:
bucket #2 size is 3 . It contains: Grey Black Yellow
bucket #3 size is 1 . It contains: Green
bucket #4 size is 0 . It contains:
bucket #5 size is 0 . It contains:
bucket #6 size is 0 . It contains:
bucket #7 size is 0 . It contains:
bucket #8 size is 2 . It contains: Brown Blue
bucket #9 size is 0 . It contains:
bucket #10 size is 0 . It contains:

Getting element bucket:
Grey is in bucket #2
Black is in bucket #2
Yellow is in bucket #2
Green is in bucket #3
Brown is in bucket #8
Blue is in bucket #8
White is in bucket #0
Red is in bucket #0

7. unordered_set member function for modifying elements

Below are the member functions for modifying elements:

insert : Insert elements
erase : Erase elements
clear : Clear content (public member function)
swap : Swap content

#include <iostream>
#include <unordered_set>
#include <algorithm>    

//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main ()
{
  std::unordered_set<std::string> myset;
  
  if(myset.empty())
  {
    std::cout << "myset is empty. Size is = " << myset.size() << std::endl;
  }
  

  myset = {"Green","Yellow","Black"};
  std::cout << "After insert size: = " << myset.size() << std::endl;

  myset.insert ("White");
  std::cout << "After another insert size: = " << myset.size() << std::endl;

  myset.erase ("White");
  std::cout << "After erase size: = " << myset.size() << std::endl;
  
  myset.clear();
  std::cout << "After clear size: = " << myset.size() << std::endl;


  return 0;
}

Output:

myset is empty. Size is = 0
After insert size: = 3
After another insert size: = 4
After erase size: = 3
After clear size: = 0

8. unordered_set member function for Hash policy

Below are the member functions for Hash policy:

load_factor : It will return load factor [load_factor = size / bucket_count ]
max_load_factor : It will get or set maximum load factor
rehash : It will set number of buckets
reserve : It will request a capacity change

 

Write a Comment

Leave a Comment

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