CKTIS / C++
std::unordered_set — a set of unique keys
std::unordered_set stores unique keys using hashing. It supports membership checks, insertion, and removal without guaranteeing iteration order. Common operations have constant average complexity but linear worst-case complexity.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <unordered_set>
std::unordered_set<Key, Hash, Pred, Allocator> values;
// Defaults: Hash = std::hash<Key>, Pred = std::equal_to<Key>,
// Allocator = std::allocator<Key>std::unordered_set from <unordered_set> is an unordered associative container: each element is also its key, and the set contains at most one element from each equivalence class. The Hash and Pred parameters define hashing and equivalence; their defaults are std::hash<Key> and std::equal_to<Key>. Equivalent keys must have the same hash; equal hashes do not imply equivalence.
insert(value).second reports whether an element was added. contains(value) tests membership, find(value) returns an iterator or end(), and erase(value) returns the number of elements removed: here, 0 or 1. Lookup, single-element insertion, and removal by key have O(1) average complexity and O(n) worst-case complexity.
Iteration order is unspecified. You cannot modify keys through iterators; instead, erase the old key and insert a new one. Rehashing invalidates iterators but not pointers or references to elements. Insertion can trigger rehashing; reserve(n) prepares enough buckets for n elements at the current maximum load factor and can also invalidate iterators. Erasing an element invalidates its iterators, pointers, and references.
The example does not print elements by iteration, so its expected output does not depend on their order. This entry targets C++23; a repository compiler flag does not attest the production runtime. The supplied working draft is updated over time and may contain features later than C++23.
Examples
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> values{3, 1, 3};
const bool inserted = values.insert(1).second;
std::cout << std::boolalpha;
std::cout << "inserted: " << inserted << '\n';
std::cout << "contains 3: " << values.contains(3) << '\n';
std::cout << "erased: " << values.erase(3) << '\n';
std::cout << "size: " << values.size() << '\n';
}
Result
inserted: false contains 3: true erased: 1 size: 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.