We have discussed about unordered_set in our previous post the problem with unordered_set is that, it is not possible to store duplicate entries in that data structure. For example if we have some value v already in unordered_set, inserting v again will have no effect.
To handle this duplication unordered_mulitset should be used, it can store duplicate elements also. Internally when an existing value is inserted, the data structure increases its count which is associated with each value. As count of each value is stored in unordered_multiset, it takes more space than unordered_set (if all values are distinct).
The internal implementation of unordered_multiset is same as that of unordered_set and also uses hash table for searching, just the count value is associated with each value in former one. Due to hashing of elements it has no particular order of storing the elements so all element can come in any order but duplicate element comes together. All operation on unordered_multiset takes constant time on average but can go upto linear in worst case.
Unordered_multiset supports many function which are demonstrated in below code :
unordered multiset 1 is not empty The size of unordered multiset 2 is : 9 3 0 5 5 7 7 2 2 3 0 5 5 7 7 7 2 2 unordered multiset 1 contains 3 5 appears 2 times in unordered multiset 1 unordered multiset 1 does not contains 9 1 appeared atleast once in unoredered_multiset 6 4 2 7 3 3 1 1 1 6 4 2 7 3 3 unordered multiset 1 is empty
As we can see most of the operation work similar to that of unordered_set but some things to note are:
equal_range(val) function returns a pair of type where first iterator points to first position of val and second points to last position of val in data structure.
erase(val) function deletes all its instances from the data structure for example if some value v has occurred t times in unordered_multiset and when erase is called, v is deleted completely which is not a expected behavior many times.
We can delete only one copy of some value by using find function and iterator version of erase, as find function returns iterator to first position of found value we can pass this iterator to erase instead of actual value to delete only one copy, the code for doing this is shown below :
6 4 2 7 3 3 1 1 1 6 4 2 7 3 3 1 1
Methods of unordered_multiset:
- insert()– Inserts new elements in the unordered_multiset. Thus increases the container size.
- begin()– Returns an iterator pointing to the first element in the container or to the first element in one of its bucket.
- end()– Returns an iterator pointing to the position immediately after the last element in the container or to the position immediately after the last element in one of its bucket.
- empty()– It returns true if the unordered_multiset container is empty. Otherwise, it returns false.
- find()– Returns an iterator which points to the position which has the element val.
- cbegin()– Returns a constant iterator pointing to the first element in the container or to the first element in one of its bucket.
- cend()– Returns a constant iterator pointing to the position immediately after the last element in the container or to the position immediately after the last element in one of its bucket.
- equal_range()– Returns the range in which all the elements are equal to a given value.
- emplace()– Inserts a new element in the unordered_multiset container.
- clear()– Clears the contents of the unordered_multiset container.
- count()– Returns the count of elements in the unordered_multiset container which is equal to a given value.
- size()– The size() method of unordered_multiset is used to count the number of elements of unordered_set it is called with.
- max_size– The max_size() of unordered_multiset takes the maximum number of elements that the unordered_multiset container is able to hold.
- swap()– Swaps the contents of two unordered_multiset containers.
- erase() – Used to remove either a single element or, all elements with a definite value or, a range of elements ranging from start(inclusive) to end(exclusive).
- bucket()– Returns the bucket number in which a given element is. Bucket size varies from 0 to bucket_count-1.
- bucket_size()– Returns the number of elements in the bucket which has the element val.
- reserve()– The reverse() function of unordered_multiset sets the number of buckets in the container (bucket_count) to the most appropriate to contain at least n elements.
- max_bucket_count()– Returns the maximum number of buckets that the unordered multiset container can have.
- load_factor()– Returns the current load factor in the unordered_multiset container.
- max_load_factor()– Returns the maximum load factor of the unordered_multiset container.
- bucket_count()– Returns the total number of buckets in the unordered_multiset container.
- hash_function()– This hash function is a unary function which takes a single argument only and returns a unique value of type size_t based on it.
- rehash()– Sets the number of buckets in the container to N or more.
- key_eq()– Returns a boolean value according to the comparison.
- emplace_hint()– Inserts a new element in the unordered_multiset container.
- get_allocator– This function gets the stored allocator object and returns the allocator object which is used to construct the container.
- operator =– The ‘=’ is an operator in C++ STL which copies (or moves) an unordered_multiset to another unordered_multiset and unordered_multiset::operator= is the corresponding operator function.
Recent Articles on unordered_multiset
This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Rust vs C++: Will Rust Replace C++ in Future ?
- Priority queue of pairs in C++ with ordering by first and second element
- Implementation of lower_bound() and upper_bound() in Vector of Pairs in C++
- Generating RGBA portable graphic images through C++
- std::basic_istream::ignore in C++ with Examples
- std::basic_istream::getline in C++ with Examples
- Format specifiers in different Programming Languages
- C program to find square root of a given number
- C program to print odd line contents of a File followed by even line content
- std::is_heap( ) in C++ with Examples
- std::basic_istream::gcount() in C++ with Examples
- new vs malloc() and free() vs delete in C++
- std::string::rfind in C++ with Examples
- Sum of factorials of Prime numbers in a Linked list
- Sum of all Palindrome Numbers present in a Linked list