![]() |
VOOZH | about |
In C++, unordered_set is an unordered associative container that stores unique elements.
5 4 3 2 1
Explanation:
In the above program, an unordered_set is initialized with the elements {1, 2, 3, 4, 5}.
Unordered set is defined as std::unordered_set class template inside the <unordered_set> header file.
unordered_set<T> us;
Parameters
An unordered_set can be declared and initialized in different ways, such as creating an empty container, using an initializer list, or constructing it from another container.
Size of us2: 4 Size of us3: 5
Explanation:
The unordered_set container provides several built-in functions for inserting, searching, traversing, and deleting elements efficiently.
New elements can be inserted into unordered set using insert() method. We cannot specify the position to insert the element as it is automatically decided by its hashed value.
2 1 3
Explanation
We canβt access elements of an unordered set by index like in an array or vector. We have to increment or decrement iterator obtained from begin() or end() methods respectively to access the element by position. This can also be done with the help of next() or advance() function.
5 3
Explanation
Elements in an unordered_set cannot be modified directly after insertion because changing a value can affect its hash.
10 3 1
Explanation
Unordered set provides fast search by value operation using the find() member function. This function returns iterator to the element if found, otherwise returns end() iterator.
4
Explanation
Unordered set can be traverse either range-based for loop or using begin() and end() iterator in a loop.
5 4 3 2 1
Explanation
Elements can be removed from the unordered set using erase() method. We can erase elements either by value or by position.
2 3 4
Explanation
Unordered set is used in many situations for different purposes.
An unordered_set stores elements using a hash table. When an element is inserted, a hash function computes its hash value and determines the bucket where the element will be stored.
Both set and unordered_set store unique elements, but they differ in how elements are organized and accessed.
| Feature | unordered_set | set |
|---|---|---|
| Internal Structure | Hash Table | Self-Balancing Binary Search Tree |
| Element Order | No specific order | Sorted order |
| Insertion | O(1) average | O(log n) |
| Deletion | O(1) average | O(log n) |
| Search | O(1) average | O(log n) |
| Duplicate Elements | Not Allowed | Not Allowed |
| Iterator Traversal | Unordered | Sorted |
The unordered_set container provides several member functions for inserting, accessing, searching, and managing elements efficiently.
Function | Description |
|---|---|
| Insert a new element in the unordered set. | |
| Return an iterator pointing to the first element in the unordered set. | |
| Return an iterator pointing to beyond the element in the unordered set | |
| Count occurrences of a particular element in an unordered set. | |
| Search for an element in the unordered set. | |
| Removes all of the elements from an unordered set and empties it. | |
| Remove either a single element or a range of elements ranging from start(inclusive) to end(exclusive). | |
| Return the number of elements in the unordered set. | |
| Exchange values of two unordered set. | |
| Insert an element in an unordered set. | |
| Returns maximum number of elements that an unordered set can hold. | |
| Check if an unordered set is empty or not. | |
| This hash function is a unary function that takes a single argument only and returns a unique value of type size_t based on it. | |
| Used to request a capacity change of unordered set. | |
| Returns the current load factor in the unordered set. | |
| Set the number of buckets in the container of unordered set to a given size or more. |