skip to content
Ivan's wiki
Site Tools
Search
Tools
Show pagesource
Backlinks
Recent Changes
Media Manager
Sitemap
>
Recent Changes
Media Manager
Sitemap
hpp-unordered-set
This page is read only. You can view the source, but not change it.
# <unordered_set> **[<unordered_set>](https://en.cppreference.com/w/cpp/header/unordered_set)** is a hash table of unique elements with O(1) average insertion, deletion, and lookup. Elements are unordered but quickly accessible. Use `unordered_set` for fast membership testing; use `std::set` if you need sorted order. This example detects duplicate values in a sequence by checking and inserting into an unordered_set, tracking first-seen items. ```cpp // compile: g++ -std=c++11 -o unorderedsetexample unorderedsetexample.cpp // run: ./unorderedsetexample // description: hash set membership testing #include <unordered_set> #include <iostream> int main() { std::unordered_set<int> seen; for (int x : {1, 2, 3, 2, 1}) { if (!seen.count(x)) { std::cout << "first time seeing " << x << "\n"; seen.insert(x); } else { std::cout << "already saw " << x << "\n"; } } return 0; } ```
Page Tools
Show pagesource
Backlinks
Back to top