C++

How to Use HashSet in C++ – Example

In computing, a HashSet is a storage container that contains a group of distinct objects. It uses hashing algorithms to store the components in a method that makes quick retrieval possible. Data is mapped using the hashing approach, which makes it simpler and quicker to find and retrieve certain pieces of information from a group of data. In this article, HashSet is analyzed with its certain functions and also the example program of C++ discusses HashSet.

What is HashSet in C++?

In C++, a HashSet is an unsorted collection of distinct elements. If you want to store the elements in a fashion that enables efficient retrieval, hashing techniques are implemented. A HashSet is identical to a set in C++.

HashSet Functions Required in C++

Several functions are available in the C++ HashSet that modify the container’s elements. The following are a few of the most used functions:

    • insert(): Adding items into the HashSet is done using the function.
    • erase(): Use this function to get rid of items from the HashSet.
    • find(): It is a function that searches the HashSet for a certain element.
    • size(): The HashSet’s size() function can be used to determine how many elements are there.
    • clear(): The clear() function is used to purge the HashSet of all its components.
    • empty(): Use the empty() function to see if the HashSet is full.

Example: Implement HashSet Functions in C++

Here is a C++ illustration showing how to utilize a HashSet:

#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
  unordered_set<int> my_HashSet;
  // Insert elements into the HashSet
  my_HashSet.insert(100);
  my_HashSet.insert(200);
  my_HashSet.insert(300);
  my_HashSet.insert(400);
  // Show the contents of the HashSet.
    cout << "HashSet elements are : ";
    for (auto i = my_HashSet.begin(); i != my_HashSet.end(); ++i) {
        cout << *i << " ";
    }
    cout << endl;
  // Find a specific element in the HashSet.
 
  if (my_HashSet.find(200) != my_HashSet.end()) {
    cout << "200 is present in the HashSet" << endl;
  } else {
    cout << "200 is not present in the HashSet" << endl;
  }
  // Delete an element from the HashSet
  my_HashSet.erase(300);
  cout << "HashSet contents after removing an element: ";
    for (auto i = my_HashSet.begin(); i != my_HashSet.end(); ++i) {
        cout << *i << " ";
    }
    cout << endl;
  // Find the size of the HashSet
  cout << "The size of HashSet after removing the element is : " << my_HashSet.size() << endl;
  // Verify whether the HashSet is empty.
  if (my_HashSet.empty()) {
  cout << "HashSet is empty" <<endl;
  } else {
    cout << "HashSet is not empty" << endl;
  }
  // Clear the HashSet of all components.
  my_HashSet.clear();
  return 0;
}

 
In this code, the program executes from the main() function, where my_HashSet of integer data type is declared. After that, four elements 100, 200, 300, and 400 are inserted with the insert() function in an unordered_set of HashSet, and all elements are shown using cout.

After this, the program searches an element 200 using the find() function with the if condition. Using erase() function, we delete the element 300 from my_HashSet and again display the element of HashSet.

Moving forward, we find the total size of our HashSet with my_HashSet.size() which is now become 3 as 300 is deleted from my_HashSet. In the end, we checked whether my_HashSet is empty or not with empty() in C++. When the program is executed, it will show the below result on the console:

Conclusion

The C++ HashSet, some of its frequently used functions, and its benefits as a tool were covered in this article. When handling unique elements in C++, the HashSet is a potent tool that can be used to build effective data structures for a variety of uses. We also provided a code that stated how to implement HashSet functions in a C++ program.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.