Last Updated : 23 Jul, 2025
Try it on GfG Practice
In C++, unordered_set is an unordered associative container that stores unique elements. Unlike set, it stores its elements using hashing. This provides average constant-time O(1) search, insert, and delete operations but the elements are not sorted in any particular order.
Example:
C++
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
// Creating an unordered_set of integers
unordered_set<int> us = {1, 2, 3, 4, 5};
for (auto x : us)
cout << x << " ";
return 0;
}
Explanation: In the above program, we created an unordered set us with values: {1, 2, 3, 4, 5}
SyntaxUnordered set is defined as std::unordered_set class template inside the <unordered_set> header file.
unordered_set<T> us;
where,
The unordered set can be declared and initialized in several ways as shown in the below example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// Create an empty unordered_set
unordered_set<int> us1;
// Initialize an unordered_set using
// using intializer list
unordered_set<int> us2 = {1, 2, 3, 4, 5};
for(auto x : us2)
cout << x << " ";
return 0;
}
Explanation: In this program:
Here are some common operations performed on an unordered set:
1. Inserting ElementsNew elements can be inserted into unordered set using insert() method. We cannot specify the position to insert the element as it is automatically decided by its hashed value.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_set<int> us;
// Insert elments using insert()
us.insert(3);
us.insert(1);
us.insert(2);
for(auto x : us)
cout << x << " ";
return 0;
}
2. Accessing Elements
We can’t access elements of an unordered set by index like in an array or vector. We have to increment or decrement iterator obtained from begin() or end() methods respectively to access the element by position. This can also be done with the help of next() or advance() function.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_set<int> us = {1, 2, 3, 4, 5};
// Accessing third element
auto it = next(us.begin(), 2);
cout << *it;
return 0;
}
Explanation: In the above code, iterator it accesses the third element by advancing begin() by 2 positions using next().
3. Updating ElementsThis operation is not very useful in unordered containers.
In unordered set, the value of the elements cannot be changed after it is inserted.
4. Finding ElementsUnordered set provides fast search by value operation using the find() member function. This function returns iterator to the element if found, otherwise returns end() iterator.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_set<int> us = {1, 2, 3, 4, 5};
// Finding 4
auto it = us.find(4);
if (it != us.end()) cout << *it;
else cout << "Element not Found!";
return 0;
}
5. Traversing
Unordered set can be traverse either range-based for loop or using begin() and end() iterator in a loop.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_set<int> us = {1, 2, 3, 4, 5};
// Using iterator in loop
for(auto it = us.begin(); it != us.end(); it++)
cout << *it << " ";
return 0;
}
5 4 3 2 1 5 4 3 2 1
Other method is to use the range based for loop that we have used in previous examples.
6. Deleting ElementsElements can be removed from the unordered set using erase() method. We can erase elements either by value or by position.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_set<int> us = {1, 2, 3, 4, 5};
// Delete element by value
us.erase(5);
// Delete element by position
us.erase(us.begin());
for(auto x : us)
cout << x << " ";
return 0;
}
Explanation: In the above example, the element with value 5 is deleted using us.erase(5). Then, the first element is deleted by position using us.erase(us.begin()).
Time ComplexityThe below table lists the time complexity of above operations on unordered set:
Operation Time Complexity Insert an element O(1) (average) Delete an element O(1) (average)Access element by position
O(n)
Find element by value O(1) (average) Traverse the set O(n) Other Basic OperationsUnordered set is used in many situations for different purposes. The following examples’ aim is to help you master unordered set beyond the basics:
Internal Working
- Check if Unordered Set is Empty
- Find the Size of Unordered Set
- Check if an Element is Present
- Swap Two Unordered Set
- Create Unordered Set for User Defined Data type
In C++, unordered_set provides the built-in implementation of hash table data structure. Each element is hashed on the basis of its value. This hash value determines where to store it in hash table. Each key in an unordered set is unique, and if an attempt is made to insert a duplicate element, it will be ignored. As it uses hashing, insertion, deletion and search operations take O(1) amortized time.
Unordered Sets vs SetFollowing is the primary differences between unordered_set and set in C++:
Here’s the list of all member functions of std::unordered_set:
Function
Description
Insert a new element in the unordered set. Return an iterator pointing to the first element in the unordered set. Return an iterator pointing to beyond the element in the unordered set Count occurrences of a particular element in an unordered set. Search for an element in the unordered set. Removes all of the elements from an unordered set and empties it. Return a const iterator pointing to the first element in the unordered set. Return a const iterator pointing to beyond the element in the unordered set. Returns the total number of elements present in a specific bucket in an unordered set. Remove either a single element or a range of elements ranging from start(inclusive) to end(exclusive). Return the number of elements in the unordered set. Exchange values of two unordered set. Insert an element in an unordered set. Returns maximum number of elements that an unordered set can hold. Check if an unordered set is empty or not. Returns range that includes all elements equal to a given value. This hash function is a unary function that takes a single argument only and returns a unique value of type size_t based on it. Used to request a capacity change of unordered set. Returns the bucket number of a specific element. Returns the total number of buckets present in an unordered set. Returns the current load factor in the unordered set. Set the number of buckets in the container of unordered set to a given size or more. Returns the current maximum load factor of the unordered set. Inserts a new element in the unordered set only if the value to be inserted is unique, with a given hint. Returns a boolean value according to the comparison. It returns the key equivalence comparison predicate used by the unordered set. Return the maximum number of buckets that unordered set can hold.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4