An unsorted assemblage comprising only distinct elements is termed a hashset in C++. The fundamental actions such as removal, presence check, and containment are part of the standard repertoire in C++. C++ also encompasses set operations like intersection, symmetric difference, and union. The hash function within a hashset plays a pivotal role in item identification and retrieval in C++. It serves as a valuable tool in pinpointing duplicates within the list. Through this hash function, not only can distinct values be obtained, but duplicate values can also be discerned. The unordered list (hashset) operates at O(1) time complexity, which is constant. Conversely, in certain scenarios, the time complexity may escalate to O(n), which is linear. This elucidates the significance and functionality of hashset in C++.
Syntax:
The format for adding a hash set or unordered set in C++, specifically for a string data type, is outlined below:
int main()
{
unordered_set <string> CBA ;
CBA.insert("") ;
CBA.insert("") ;
..................
}
Some examples of C++ hashset with their working mechanism:
An unordered_set or HashSet is a set in which the key is stored in any order. For a HashSet, there are many functions used. But the most commonly used functions are stated below:
- The size function is used for capacity.
- empty function is also used for capacity.
- find is used to search for a key.
- Erase function is used for modification in it.
- The insert function is also used for modification.
An unorderedset permits solely distinct keys, while an unorderedmultiset permits solely repeated keys to traverse it.
Examples:
With various illustrations, the complete operational concept of C++ HashSet has been elucidated below:
1) An example of a C++ hash set using curly braces for an initialized list:
Using HashSet in C++, a fundamental example is presented where the set is initialized using an initializer list {…..}.
Code:
#include <iostream>
#include <unordered_set>
int main()
{
std::unordered_set<int> P { 2017, 2016, 2015 };
for (auto Q: P)
std::cout << Q << '\n';
return 0;
}
Output:
2015
2016
2017
Utilization of binary predicate for comparing objects during the process:
By employing a binary predicate set, the comparison entities are provided in the following instance. The arrangement of the set is established based on a pair of identical elements.
Code:
#include <iostream>
#include <set>
struct JAVACPPTUTORIAL {
template<typename X>
bool operator()(const X& n, const X& p) const
{
return n > p;
}
};
int main()
{
std::set<int, JAVACPPTUTORIAL> values = { 120, 80, 250 };
for (auto S: values)
std::cout << S << '\n';
return 0;
}
Output:
250
120
80
3) An illustration of a hash set in C++ showcasing the application of insert, iteration, locate, and declaration:
In the provided scenario, the insert, erase, and find operations all have an average time complexity of constant time. Within the example, the find function behaves in a specific manner when the key is absent in the set, returning an Iterator pointing to the end position. Conversely, when the key is present in the set, the Iterator easily navigates back to the key's position. In cases where the key values are stored as pointers, the Iterator is employed to access the key, and the key itself can be retrieved by dereferencing the * operator.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_set <string> CBA ;
CBA.insert("Developer") ;
CBA.insert("Programmer") ;
CBA.insert("tester") ;
CBA.insert("HR") ;
CBA.insert("Coder") ;
string key = "JAVACPPTUTORIAL" ;
if (CBA.find(key) == CBA.end())
cout << key << " one of the best company." << endl << endl ;
else
cout << "retrieved" << key << endl << endl ;
key = "Programmer";
if (CBA.find(key) == CBA.end())
cout << key << "can not retrieve\n" ;
else
cout << "retrieved " << key << endl ;
cout << "\nhere is the designations : " <<endl;
unordered_set<string> :: iterator itr;
for (itr = CBA.begin(); itr != CBA.end(); itr++)
cout << (*itr) << endl;
}
Output:
JAVACPPTUTORIAL one of the best company.
retrieved Programmer
here is the designations :
HR
tester
Programmer
Coder
Developer
When the key data is not found in the order list:
JAVACPPTUTORIAL one of the best company
Program can not retrieve
here is the designations :
HR
tester
Programmer
Coder
Developer
4) Employing an unsorted set to search for duplicate information:
In the following example, a collection of integers is presented, and within this collection, duplicate values have been identified and showcased in the result.
Code example:
#include <bits/stdc++.h>
using namespace std;
void printDuplicates(int deepak[], int M)
{
unordered_set<int> JAVACPPTUTORIAL;
unordered_set<int> similar;
for (int P = 0; P < M; P++)
{
if (JAVACPPTUTORIAL.find(deepak[P]) == JAVACPPTUTORIAL.end())
JAVACPPTUTORIAL.insert(deepak[P]);
else
similar.insert(deepak[P]);
}
cout << "similar contents are : ";
unordered_set<int> :: iterator start;
for (start = similar.begin(); start != similar.end(); start++)
cout << *start << " ";
}
int main()
{
int deepak[] = {9, 3, 6, 1, 6, 2, 4, 9, 5, 7, 0, 8};
int M = sizeof(Deepak) / sizeof(int);
printDuplicates(Deepak, M);
return 0;
}
Output:
similar contents are : 9 6
Conclusion:
In the preceding discussion, we explored the concept of HashSet in C++ and how it functions. Throughout this guide, we also delved into the diverse uses of C++ HashSet, supported by various illustrations showcasing its functionality. When it comes to identifying duplicate data and retrieving specific information, the C++ HashSet proves to be an indispensable tool.