C++ std::unordered_set Functions

Beginner
8 min read
Updated: Nov 2026
31 function guides
Function index

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 hash table of unique values.

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:

C++
#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 functions

Ask how many unique values an unordered_set holds.

FunctionDescriptionTutorial
empty()Check whether an unordered_set has no values at all.Try it
size()Count how many unique values are in the unordered_set.Try it
max_size()Get some number of values an unordered_set could ever hold.Try it

Modifiers

9 functions

Add, move, or remove unique values. Duplicates are ignored. Values are not kept sorted.

FunctionDescriptionTutorial
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 functions

Search for a value in average constant time. There is no lower_bound/upper_bound because values are not sorted.

FunctionDescriptionTutorial
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 functions

Inspect and tune the hash table buckets, load factor, rehash, and reserve.

FunctionDescriptionTutorial
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 functions

Read the hash function, key equality predicate, and allocator.

FunctionDescriptionTutorial
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 functions

Walk through the values in an unspecified order. The c versions are read-only. unordered_set has no reverse iterators.

FunctionDescriptionTutorial
begin()Get an iterator to one value in the unordered_set.Try it
end()Get the iterator just past the last value.Try it
cbegin()Get a read-only iterator to one value (C++11).Try it
cend()Get a read-only iterator past the last value (C++11).Try it

Frequently Asked Questions

They are the functions you call on a std::unordered_set object with a dot, such as s.insert(5), s.find(5), s.erase(5), or s.reserve(100). They need #include <unordered_set>.
This tutorial series covers 31: capacity (empty, size, max_size), modifiers (clear, insert, insert_range, emplace, emplace_hint, erase, extract, merge, swap), lookup (count, find, contains, equal_range), the bucket interface (bucket_count, rehash, reserve, load_factor, ...), observers (hash_function, key_eq, get_allocator), and forward iterators (begin/end, cbegin/cend).
An unordered_set stores only unique values. The value is its own key, so there is nothing to look up and return as a mapped value. Use find(), count(), or contains() to test membership.
emplace() and emplace_hint() are C++11. extract() and merge() are C++17. contains() is C++20. insert_range() is C++23.
Read the C++ std::unordered_set page first. Then open insert(), find(), contains(), reserve(), and erase() from the tables below.

Start with insert()

Add a unique value to an unordered_set. Duplicates are ignored.

insert() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful