Unordered_sets in C++ are containers that similar to sets. They store unique elements in any order. The elements inside the unordered_set cannot be changed, once they are added to the set, they can only be inserted or deleted. Insertion in unordered_set is randomized.
They are implemented as a hash table in memory. The element in the unordered_set acts as both key and value in the hash table. All operations take O(1) time on average and O(N) time in the worst case. Unordered_set is present in #include<unordered_set> header file. The elements inside the unordered_set can be accessed using iterators.
Unordered_set Declaration
Syntax:
unordered_set <data_type> name = {initial_values};
data_type: data type of the elements to be stored inside the unordered_set
initial_values: optional parameter which initializes the unordered_set with the given values
Example:
unordered_set <int> s; //initializes a unordered_set of size 0 which stores integer values
unordered_set <int> s = {10, 20, 30}; //initializes a unordered_set having initial values as 10,20,30Functions on unordered_sets
begin(): Returns an iterator to the first element of the unordered_set.
Parameters: None
Return type: iterator
end(): Returns an iterator to the element past the last element of the unordered_set.
Parameters: None
Return type: iterator
size(): It tells us the size of the unordered_set.
Parameters: None
Return type: integer - total number of elements in the unordered_set
insert(element): Inserts an element in the unordered_set.
Time Complexity: O(1) in the average case and O(N) in the worst case where N is the size of the unordered_set
Parameters: the element to be inserted
Return type: void
erase(value)orerase(start_iterator,end_iterator): Delete elements from the unordered_set.
Time Complexity: O(1) in the average case and O(N) in the worst case where N is the size of the unordered_set
Parameters: the value to be removed or iterators pointing to the position between which the value needs to be deleted
Return type: void
find(element): Returns an iterator pointing to the element, if the element is found else returns an iterator pointing to the end of the unordered_set.
Parameters: the element which needs to be found
Return type: iterator
clear(): It deletes all the elements from the unordered_set.
Parameters: None
Return type: void
empty(): It tells us whether the unordered_set is empty or not.
Parameters: None
Return type: Boolean, true if a unordered_set is empty else false
#include<iostream>
#include<unordered_set>
using namespace std;
int main() {
unordered_set <int> s;
for (int i = 0; i < 5; i++) {
s.insert(i + 1);
}
s.insert(0);
unordered_set <int> ::iterator it;
cout << "Unordered_set ";
for (it = s.begin(); it != s.end(); it++)
cout << * it << " ";
cout << '\n';
s.erase(1);
s.erase(s.find(2));
cout << "After erasing element, size of unordered_set is " << s.size() << '\n';
int val = 4;
if (s.find(val) != s.end())
cout << "The unordered_set contains " << val << endl;
else
cout << "The unordered_set does not contains " << val << endl;
cout << "Elements of unordered_set ";
for (it = s.begin(); it != s.end(); it++)
cout << * it << " ";
cout << '\n';
s.clear();
if (s.empty() == true) {
cout << "set is empty now!";
}
return 0;
}Output
Unordered_et 0 2 1 3 4 5
After erasing element, size of unordered_set is 4
The unordered_set contains 4
Elements of unordered_set 0 3 4 5
set is empty now!