In C++, std::unordered_set<T> is a container that stores unique values in a hash table. If you add a value that is already inside, the unordered_set quietly ignores it. Values are not kept sorted. Iteration order is unspecified.
Finding, inserting, and erasing a value are usually average constant time. Check for a value with find() or contains(), add values with insert() or emplace(), and call reserve() before many inserts. Include <unordered_set> to use it.
This page teaches what a std::unordered_set is. The Unordered Set Functions index lists every function guide in this series.
Why choose an unordered_set?
Choose an unordered_set when you only care whether a value is present, want no duplicates, and do not need sorted output, for example unique user ids, visited urls, or a fast membership filter. If you need sorted output or lower_bound-style searches, use std::set. If each key needs an attached value, use std::unordered_map. If you only need a list by position, use a std::vector.
Syntax
C++ Syntax
Common ways to create and use a std::unordered_set:
C++
#include <iostream>
#include <string>
#include <unordered_set>
std::unordered_set<int> a; // empty
std::unordered_set<int> b = {30, 10, 20, 10}; // unique values, order unspecified
std::unordered_set<std::string> d = {"pear", "apple"};
b.reserve(10); // prepare buckets
b.insert(25); // add only if the value is new
b.emplace(5); // build the value in place
bool has = b.contains(20); // true (C++20)
auto it = b.find(30); // iterator, or b.end()
b.erase(10); // remove by value
std::size_t n = b.size(); // number of values
Reference
std::unordered_set vs std::set vs std::vector vs std::unordered_map
Here is a quick comparison of containers that beginners often mix up:
Feature
std::unordered_set
std::set
std::vector
std::unordered_map
Stores
Unique values
Unique values
Values (duplicates OK)
Key and value pairs
Order of elements
Unspecified (hash buckets)
Sorted by value
The order you added them
Unspecified (hash buckets)
Find a value / key
Usually constant
Logarithmic
Linear
Usually constant (by key)
Duplicates
Not allowed
Not allowed
Allowed
Unique keys
Ordered search (lower_bound())
No
Yes
Only if you sort it
No
Header
<unordered_set>
<set>
<vector>
<unordered_map>
Guide
When to Use find(), contains(), insert() or reserve()
Several members test or change membership. Pick the one that matches your goal:
std::unordered_set<T> is a hash table from <unordered_set>. It stores unique values, finds a value in average constant time, and does not keep values sorted. Iteration order is unspecified.
unordered_set uses hashing, is usually faster to find a value, and does not sort values. set keeps values sorted and finds in logarithmic time. Choose set when you need sorted output or lower_bound-style range searches.
unordered_set stores only unique values. unordered_map stores key and value pairs. Use a map when each key needs an attached value.
No. The elements are const, because changing a value could break the hash table. To change one, erase the old value and insert the new one, or use extract() in C++17 to move the value out and edit it.
Open the C++ std::unordered_set Functions index. It lists every member function in this series, from insert() and find() to reserve(), rehash(), and hash_function().