C++ std::unordered_set Functions

About This Index
This page is the C++ std::unordered_set Functions index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs.
Learn what a std::unordered_set is on /cpp/unordered-set. Use this page when you need a specific member function such as insert(), find(), or reserve().
A std::unordered_set<T> stores unique values with hashing, allows each value only once, and does not keep values sorted. Elements are const. There is no operator[] and no at(). Call reserve() before many inserts. The 31 functions are listed below.
How to Call an Unordered Set Function
Include <unordered_set>, create the unordered_set, and call members with a dot:
#include <iostream>
#include <unordered_set>
#include <string>
std::unordered_set<int> s = {30, 10, 20, 10}; // unique: 10, 20, 30 (order unspecified)
s.reserve(20); // prepare buckets
s.insert(25); // add only if new
s.emplace(5); // build in place
auto it = s.find(20); // iterator, or s.end()
bool has = s.contains(99); // false (C++20)
s.erase(10); // remove by value
std::size_t n = s.size(); // number of values C++ std::unordered_set Functions Table
All function guides in this C++ tutorial series (31 tutorials), grouped by purpose.
Capacity
3 functionsAsk how many unique values an unordered_set holds.
Modifiers
9 functionsAdd, move, or remove unique values. Duplicates are ignored. Values are not kept sorted.
| Function | Description | Tutorial |
|---|---|---|
clear() | Remove every value so the unordered_set becomes empty. | Try it |
insert() | Add a value to the unordered_set. A repeated value is ignored. | Try it |
insert_range() | Add every value of a range in one call (C++23). | Try it |
emplace() | Build a value directly inside the unordered_set (C++11). | Try it |
emplace_hint() | Build a value in place, using a hint for its position (C++11). | Try it |
erase() | Remove a value, one position, or a range from the unordered_set. | Try it |
extract() | Take a value out of the unordered_set as a node you can edit or move (C++17). | Try it |
merge() | Move all new values from another unordered_set into this one (C++17). | Try it |
swap() | Swap all values with another unordered_set of the same type. | Try it |
Lookup
4 functionsSearch for a value in average constant time. There is no lower_bound/upper_bound because values are not sorted.
| Function | Description | Tutorial |
|---|---|---|
count() | Tell whether a value exists: the answer is 0 or 1 for an unordered_set. | Try it |
find() | Search for a value and get an iterator to it, or end(). | Try it |
contains() | Return true if the value is in the unordered_set (C++20). | Try it |
equal_range() | Get the range of values equal to the given value. | Try it |
Bucket interface
8 functionsInspect and tune the hash table buckets, load factor, rehash, and reserve.
| Function | Description | Tutorial |
|---|---|---|
bucket_count() | How many hash buckets the unordered_set currently has. | Try it |
max_bucket_count() | The largest number of buckets this unordered_set type could have. | Try it |
bucket_size() | How many elements sit in one hash bucket. | Try it |
bucket() | Which bucket a value would use (or already uses). | Try it |
load_factor() | Average number of elements per bucket right now. | Try it |
max_load_factor() | Get or set the load factor limit that triggers growth. | Try it |
rehash() | Rebuild the table with at least n buckets. | Try it |
reserve() | Prepare buckets for about n elements (avoids rehashes). | Try it |
Observers
3 functionsRead the hash function, key equality predicate, and allocator.
| Function | Description | Tutorial |
|---|---|---|
hash_function() | Get the hash function object the unordered_set uses on values. | Try it |
key_eq() | Get the value equality predicate the unordered_set uses. | Try it |
get_allocator() | Get a copy of the allocator the unordered_set uses for its nodes. | Try it |
Iterators
4 functionsWalk through the values in an unspecified order. The c versions are read-only. unordered_set has no reverse iterators.
Frequently Asked Questions
Start with insert()
Add a unique value to an unordered_set. Duplicates are ignored.
12 people found this page helpful
