INTRODUCTION
UNORDERED SET in Cpp
In this tutorial, we are going to learn about unordered-set in C++ from the standard template library. An unordered set is also the same as a normal set in which unique elements are stored. Unlike a set, elements are not stored in some specific order likewise elements are stored in ascending order like in a set. Prior to storing elements in specific sorted order, everything is the same as a normal set. The unordered set is implemented using a hash table where keys are hashed into indices of a hash table so that insertion is always accessed randomly.
The syntax of the unordered set is.
Unordered_set < datatype > unordered_set_name;
* average time complexity of any kind of operation in unordered_set takes an average of O( 1 ) and the worst case is O( n ) which depends on the internal hash function, but hashing performs very well and generally provides constant time complexity.
let’s look at some of the basic methods that are performed on the unordered_set.
insert(): this method is used to insert a new element in the unordered_set.
erase(): this method is used to remove an element or a range of elements ranging from the
begin value to the end value from the unordered_set.
begin(): it returns an iterator that points to the first element in the unordered_set.
end(): it returns an iterator that points to the last element in the unordered_set.
count(): this method returns the number of occurrences of a particular element in an unordered_set.
size(): this method is used to return the number of elements in a container.
swap(): this method is used to swap values of two unordered_sets.
find(): this method is used to search for an element in a container.
clear(): this method is used to remove all the elements from the container and empties the particular unordered_set.
empty(): this is a method that returns a boolean value whether the particular unordered_set is empty or not.
These are some of the basic methods that are performed on the unordered_set.
We can initialize values to the unordered_set using basic unordered_set methods or by direct initialization of the values using curly braces {}.
Let’s look at a sample program implementing an unordered_set container.
#include <iostream>
#include <iterator>
#include <unordered_set>
using namespace std;
main() {
unordered_set<int> s1;
s1.insert(40);
s1.insert(30);
s1.insert(60);
s1.insert(20);
s1.insert(50);
s1.insert(10);
s1.insert(50); // adding 50 two times
unordered_set<int>::iterator itr;
cout << "unordered_set is : \n";
for (itr = s1.begin(); itr != s1.end(); itr++) {
cout << *itr << " ";
}
cout<<"\nsize of the unordered set : \n"<<s1.size()<<endl;
cout<<"unordered_set after removing 10 : \n";
s1.erase(10);
for (itr = s1.begin(); itr != s1.end(); itr++) {
cout << *itr << " ";
}
}
The output of the program is.
In the above program, we have included the < unordered_set > header to create unordered_set and perform basic operations on it. we have created an unordered_set s1 and inserted elements using the insert() function. Here we see that the elements in the unordered_set are stored in the order in which they are inserted. It is the only difference from the normal set where elements are stored in some specific sorted order. We found the size of the unordered_set using the size() function and remove one of the elements from the unordered_set using erase() function
Let’s look at the differences between the normal set and the unordered set.
SET | UNORDERED SET |
The set stores elements in some specific sorted order. | An unordered set stores elements in the order in which they are inserted. |
The set stores unique elements only. | An unordered set also stores unique elements like set. |
The set follows binary search tree implementation. | An unordered set follows hash tables for implementation. |
Syntax: set<datatype> set_name; | Syntax:Unordered_set< datatype > unordered |
CONCLUSION
That’s it from this tutorial. We have learned about the unordered_set container, its properties, different kinds of basic methods implemented on unordered_sets and the differences between normal sets and unordered sets. Hope you guys found it interesting. Happy Coding!