C++ unordered_set Library
C++ unordered_set Functions
The <unordered_set> library has functions that let you add, find and remove elements in an unordered set. An unordered set stores unique values. Unlike a set, the values are not sorted, and they come in no particular order.
Many of the functions return an iterator (a pointer-like object that points to an element). Use *it to get the value that the iterator points to.
An unordered set keeps its values in buckets (groups of values). It calculates a number from each value (a hash) to choose the bucket. This is why it is fast to find a value. Use bucket_count(), load_factor() and reserve() to see and control the buckets.
| Function | Description |
|---|---|
| begin() | Returns an iterator to the first element of an unordered set |
| bucket_count() | Returns the number of buckets (groups that hold the elements) in an unordered set |
| clear() | Removes all elements from an unordered set |
| count() | Returns 1 if a value exists in an unordered set, and 0 if it does not |
| emplace() | Adds a new element to an unordered set, built directly inside the set |
| empty() | Checks whether an unordered set is empty or not |
| end() | Returns an iterator to the position after the last element of an unordered set |
| erase() | Removes one or more elements from an unordered set |
| find() | Searches for a value, and returns an iterator to the element |
| insert() | Adds one or more elements to an unordered set |
| load_factor() | Returns the average number of elements in each bucket of an unordered set |
| reserve() | Makes room for at least a given number of elements in an unordered set |
| size() | Returns the number of elements in an unordered set |
| swap() | Swaps the contents of two unordered sets |