How to Declare Unordered Sets in C++
The implementation of an unordered set using a hash table ensures that the insertion is always randomised by hashing the keys into hash table indices. When we define keys of user-defined data structures, we must describe our comparison function under how keys will be compared.
Unordered set methods
Numerous functions are defined for unordered sets. The Unordered Set supports unique keys. An unordered multiset should be used for duplicate keys.
Following is an example of a declaration, find, insert, and iteration in an unordered set:
Example 1:
// A C++ program that demonstrates the unordered Set's numerous features
#include <bits/stdc++.h>
usingnamespace std;
int main()
{
// setting up a set to hold the string data type
unordered_set <string> stringSet ;
// the identical string will be stored after each inserted line, once in a group.
stringSet.insert("code") ;
stringSet.insert("in") ;
stringSet.insert("c++") ;
stringSet.insert("is") ;
stringSet.insert("fast") ;
string key = "slow" ;
// If the key cannot be found, find produces an end iterator.
// if not, an iterator to that key is returned.
if (stringSet.find(key) == stringSet.end())
cout << key << " not found" << endl << endl ;
else
cout << "Found " << key << endl << endl ;
key = "c++";
if (stringSet.find(key) == stringSet.end())
cout << key << " not found\n" ;
else
cout << "Found " << key << endl ;
// presently iterating through the entire Set and outputting its content
cout << "\nAll elements : ";
unordered_set<string> :: iterator itr;
for (itr = stringSet.begin(); itr != stringSet.end(); itr++)
cout << (*str) << endl;
}
Output:
Find, insert, and erase generally take the same amount of time. Finding all the duplicates within an array (list) of integers is a real-world problem based on an unordered set.
The C++ approach utilizing unordered sets is shown below.
Example 2:
// Using a C++ application, locate duplicates in an array
Unordered Set
#include <bits/stdc++.h>
usingnamespace std;
// Utilizing unordered set, print duplicates in arr[0..n-1]
void printDuplicates(int arr[], int n)
{
// creating unordered collections to be used for storing and checking duplicates
unordered_set<int> intSet;
unordered_set<int> duplicate;
// looping through the items in an array
for (int i = 0; i < n; i++)
{
// if an element is missing, add it.
if (intent.find(arr[i]) == intent.end())
intent.insert(arr[i]);
// Insert into duplicate Set if the element already exists.
else
duplicate.insert(arr[i]);
}
// publishing the outcome
cout << "Duplicate item are: ";
unordered_set<int> :: iterator itr;
// From begin() to finish, iterator loops ()
for (it = duplicate.begin(); it != duplicate.end(); itr++)
cout << *it << " ";
}
int main()
{
int arr[] = {1, 5, 2, 1, 4, 3, 1, 7, 2, 8, 9, 5};
int n = sizeof(arr) / sizeof(int);
printDuplicates(arr, n);
return 0;
}
Output:
Techniques for Unordered Sets
- Insert a new "element" into the unordered set container with insert ().
- Return the iterator for the first element in the unordered set container with the begin() function.
- End() produces an iterator that navigates to the piece after the end.
- Count() function counts how many times a specific feature appears in an unordered set container.
- Find a part in the container using the find() function.
- Clear() Empties an unordered set by removing all its components.
- Return the first element in the unordered set container as a const iterator with the help of the begin() function.
- Return a const iterator pointing to an element past the end of the unordered set container or a bucket within it with the lot() function.
- Return the overall number of elements in a particular bucket within an unordered set container.
- Remove one element or a group of items from the start (inclusive) to the finish using the erase() function (exclusive).
- Size() will give the container's unordered set element count.
- Swap two unordered set containers' values with this function.
- Add an element to an unordered set container using the emplace() method.
- An unordered set container's maximum number of elements can be found using the max size() function.
- Check whether an unordered set container is empty using the empty() method.
- Returns a range containing all elements with values equal to the specified value.
- operator= - Moves or copies an unordered set from one unordered Set to another.
- The analogous operator function is operator=.
- Hash function is a unary function that only accepts a single parameter and outputs a single size t value.
- Reserve() asks for an unordered set's capacity to be changed.
- Return the element's bucket number using the bucket() function.
- An unordered set container's total number of buckets is returned by the bucket count() function.
- Return the current load factor for the unordered set container through load factor().
- Set the number of buckets in the unordered set container to the specified size or more significant with the rehash() function.
- The maximum load factor of the unordered set container is returned or set by the max load factor() function.
- If the value to be inserted is unique, and a hint is provided, the function emplace hint() will only add a new element to the unordered Set.
- Operator == When two unordered sets are compared side by side using the '==' operator in the C++ STL, unordered Set:: The analogous operator function for the same is operator==.
- Essential eq() provides a Boolean result based on the comparison. It gives back the unordered Set's necessary equivalence comparison predicate.