C++ Library - <unordered_multiset>



Introduction

An unordered_multiset is an associative container for storing elements and has no particular order unlike multiset that stores elements in ascending order. It uses hashing for fast access. It accepts duplicate elements just like multiset.

The elements can not be modified once added to the unordered_multiset. The keys are treated as const and the elements can be inserted and removed from the container. Since hash tables are used, an unordered_multiset provides faster average-time access, insertion, and deletion compared to multiset.

Characteristics of Unordered Multiset

The key characteristics of an unordered_multiset are given below −

  • An unordered_multiset is implemented using hash tables.
  • In unordered multiset, there is no specific order to store elements unlike a multiset.
  • Duplicate elements are allowed in an unordered_multiset.
  • It has constant (O(1)) average time for inserting, deleting, and searching operations and O(n) in worst case when many hash collisions occur.
  • Elements in an unordered_multiset are immutable.
  • It maps keys to buckets using a hash function for efficient access.

Definition

Below is definition of std::unordered_multiset −

 template< class Key, class Hash = std::hash<Key>, class Pred = std::equal_to<Key>, class Alloc = std::allocator<Key> > class unordered_multiset; 

Parameters

Below is the description of parameters used in the unordered multiset −

  • Key − It defines the type of element.
  • Hash − It is a unary function object.
  • Pred − It is a binary predicate that takes two arguments of the same type as the elements and returns a bool.
  • Alloc − It defines the type of allocator.

Member Types

Following member types can be used as parameters or return type by member functions.

Member Type Definition
key_type It is the first template parameter (Key)
value_type It is the first template parameter (Key)
hasher It is the second template parameter (Hash)
key_equal It is the third template parameter (Pred)
allocator_type It is the fourth template parameter (Alloc)
reference Alloc::reference
const_reference Alloc::const_reference
pointer Alloc::pointer
const_pointer Alloc::const_pointer
iterator A forward iterator to const value_type
const_iterator A forward iterator to const value_type
local_iterator A forward iterator to const value_type
const_local_iterator A forward iterator to const value_type
size_type An unsigned integral type
difference_type A signed integral type

Member functions

Below is list of member functions −

Method Description
Constructor Constructs the unordered multiset container.
Destructor Destructs the unordered_multiset
operator= It is used to assign the values to the container.

Capacity

Method Description
empty It is used to test whether container is empty.
size It returns container size.
max_size It returns maximum size.

Iterators

Method Description
begin It returns iterator to beginning.
end It returns iterator to end.
cbegin It returns const_iterator to beginning.
cend It returns const_iterator to end.

Lookup

Method Description
find It is used to get iterator to element.
count It is used to count elements with a specific key. Can return values greater than 1.
contains It checks if an element exists in the unordered multiset container or not.
equal_range It is used to get range of elements with a specific key. Particularly useful for retrieving all duplicate elements.

Modifiers

Method Description
emplace It is used to construct and insert element. Always succeeds since duplicates are allowed.
emplace_hint It is used to construct and insert element with hint.
insert It is used to insert elements. Always succeeds since duplicates are allowed.
insert_range It is used for inserting a specified range of elements in the unordered multiset.
erase It is used to erase elements.
clear It is used to clear content.
swap It is used to swap content.
extract It is used for extracting nodes from the unordered multiset container.
merge It is used to merge elements from another container.

Buckets

Method Description
begin It returns an iterator that points to the beginning of the specified bucket.
end It returns an iterator that points to the end of the specified bucket.
cbegin It returns a const_iterator that points to the beginning of the specified bucket.
cend It returns a const_iterator that points to the end of the specified bucket.
bucket_count It returns number of buckets.
max_bucket_count It returns maximum number of buckets.
bucket_size It returns bucket size.
bucket It locates element's bucket.

Hash policy

Method Description
load_factor It returns load factor.
max_load_factor It is used to get or set maximum load factor.
rehash It is used to set number of buckets.
reserve It gives a request to capacity change of buckets.

Observers

Method Description
hash_function It is used to get hash function.
key_eq It is used to get key equivalence predicate.
get_allocator It is used to get allocator.

Non-member overloaded functions

Method Description
operator== Tests whether two unordered_multisets are equal or not.
operator!= Tests whether two unordered_multisets are unequal.
swap It exchanges contents of two unordered_multiset containers.
erase_if It is used for erasing the elements that satisfies the specified condition.

Examples of Unordered Multiset

The following example demonstrates the usage of unordered_multiset in C++ STL:

Inserting and Printing Elements in Unordered Multiset

In this example, we have created an unordered_multiset of integers and performed various operations like insertion, counting elements, and displaying the contents.

 #include <iostream> #include <unordered_set> using namespace std; int main(){ unordered_multiset<int> nums; nums.insert(5); nums.insert(2); nums.insert(5); nums.insert(3); cout << "Elements in unordered multiset: "; for (int x : nums) cout << x << " "; } 

The output of the above code is given below −

 Elements in unordered multiset: 3 5 5 2 

Checking Occurrence of an Element

Here is an example of checking Occurrence of duplicate elements −

 #include <iostream> #include <unordered_set> using namespace std; int main(){ unordered_multiset<int> nums = {10, 20, 10, 30, 10}; cout << "Elements in unordered multiset: "; for (int x : nums) cout << x << " "; cout << endl; cout << "Count of 10:" << nums.count(10) << endl; } 

The output of the above code is given below −

 Elements in unordered multiset: 30 20 10 10 10 Count of 10:3 

Erasing All Occurrence of Element

In this example, we are erasing all the occurrences of specified element −

 #include <iostream> #include <unordered_set> using namespace std; int main() { unordered_multiset<int> nums = {5, 5, 2, 3, 5}; cout << "Original elements: "; for (int x : nums) cout << x << " "; cout << endl; nums.erase(5); cout << "Elements after erasing 5: "; for (int x : nums) cout << x << " "; } 

The output of the above code is given below −

 Original elements: 3 2 5 5 5 Elements after erasing 5: 3 2 
Advertisements