Unordered multisets are containers that store elements in no particular order, allowing fast retrieval of individual elements based on their value, much like unordered_set containers, but allowing different elements to have equivalent values. In an unordered_multiset, the value of an element is at the same time its key, used to identify it. Keys are immutable, therefore, the elements in an unordered_multiset cannot be modified once in the container – they can be inserted and removed, though.

 

Important methods

  • iterator begin() noexcept;
    const_iterator begin() const noexcept;
    Returns an iterator pointing to the first element in the unordered_multiset container.
  • void clear() noexcept;
    All the elements in the unordered_multiset container are dropped: their destructors are called, and they are removed from the container, leaving it with a size of 0.
  • size_type count ( const key_type& k ) const;
    Searches the container for elements with a value of k and returns the number of elements found.
  • bool empty() const noexcept;
    Returns a bool value indicating whether the unordered_multiset container is empty, i.e. whether its size is 0.
  • iterator end() noexcept;
    const_iterator end() const noexcept;
    Returns an iterator pointing to the past-the-end element in the unordered_multiset container.
  • (1) iterator erase ( const_iterator position );
    (2) size_type erase ( const key_type& k );
    (3) iterator erase ( const_iterator first, const_iterator last );
    Removes from the unordered_multiset container either the elements whose value is k or a range of elements ([first,last)).
  • iterator find ( const key_type& k );
    const_iterator find ( const key_type& k ) const;
    Searches the container for an element with k as key and returns an iterator to it if found, otherwise it returns an iterator to unordered_multiset::end (the element past the end of the container).
  • size_type size() const noexcept;
    Returns the number of elements in the unordered_multiset container.

 

Example

#include <iostream>
#include <iterator>
#include <unordered_set>
 
using namespace std;
 
int main(){

  //  Initialization by intializer list 
  unordered_multiset<int> ums2 ({1, 3, 1, 7, 2, 3, 4, 1, 6}); 
  unordered_multiset<int> ums;
  unordered_multiset<int> :: iterator it;
  
  // Directly intializing
  ums = {1,2,3,4,5};

  // Inserting a single element
  ums.insert(100); 
  
  // Elements in the unordered multiset
  for(it= ums.begin(); it!=ums.end(); ++it) {
    cout << *it;
  }

  // Searching '3'
  it = ums.find(3);
  
  // Size of the unordered multiset
  cout << ums.size();
  
  //  Total number of occurence in set 
  cout << ums.count(val); 
         
  // Erasing element 5
  ums.erase(5);
  
  // Clear opeartin make unordered multiset empty.
  ums.clear();
  
  bool flag= ums.empty();
  if (flag ==1)
    cout << "Unordered multiset is empty " << endl;
  else
    cout << "Unordered multiset is not empty " << endl;
  
  return 0;
}