unordered_set
std::unordered_set is an associative container that contains a set of unique objects. Search, insertion, and removal operations have average constant-time complexity.
Initialization
std :: unordered_set < int > myUnorderedSet ;
Insert Elements
Insert Single Element (Average case O(1)):
myUnorderedSet . insert ( 10 ); // Insert an element
Emplace Element (Average case O(1)):
myUnorderedSet . emplace ( 10 ); // Construct and insert element
Access Elements
Find Element (Average case O(1)):
auto it = myUnorderedSet . find ( 10 );
// Returns iterator to the element if found, otherwise returns myUnorderedSet.end()
Removing Elements
Remove Element by Value (Average case O(1)):
myUnorderedSet . erase ( 10 ); // Erases element with value 10
Query Attributes
Get Unordered Set Size (O(1)):
size_t size = myUnorderedSet . size (); // Returns the number of elements
Check if Unordered Set is Empty (O(1)):
bool isEmpty = myUnorderedSet . empty (); // Returns true if unordered set is empty, otherwise false
Iterating through Unordered Set
for ( auto it = myUnorderedSet . begin (); it != myUnorderedSet . end (); ++ it ) {
// Access the element as *it
}
Using Range-based For Loop (O(n)):
for ( const auto & elem : myUnorderedSet ) {
// Access the element directly as elem
}
Other Common Operations
Clear Unordered Set (O(n)):
myUnorderedSet . clear (); // Removes all elements from the unordered set
May 8, 2026 14:06:20
May 8, 2026 14:06:20