C++20 sections · 1024 units
Open in Course

Set vs Unordered Set

Two set types

The set container keeps elements sorted and uses a balanced tree internally. All operations take O(logn)O(\log n) time. Use it when you need ordered iteration or range queries. The unordered_set uses hashing for O(1)O(1) average time operations.

Elements have no particular order when iterating. Include <unordered_set> to use this faster variant. Choose based on your needs. For simple membership tests where order doesn't matter, unordered_set is faster.

For sorted output or finding ranges, stick with ordered set.