Multisets are containers that store elements following a specific order, and where multiple elements can have equivalent values. In a multiset, the value of an element also identifies it (the value is itself the key, of type T). The value of the elements in a multiset cannot be modified once  in the container (the elements are always const), but they can be inserted or removed from the container. If we want to update element then we should delete that element and again insert with updated element. The elements in the multiset are always sorted.

Important functions

  • iterator begin() noexcept;
    const_iterator begin() const noexcept;
    Returns an iterator referring to the first element in the multiset container.
  • void clear() noexcept;
    Removes all elements from the multiset container (which are destroyed), leaving the container with a size of 0.
  • size_type count (const value_type& val) const;
    Searches the container for elements equivalent to val and returns the number of matches.
  • bool empty() const noexcept;
    Returns whether the multiset container is empty (i.e. whether its size is 0).
  • iterator end() noexcept;
    const_iterator end() const noexcept;
    Returns an iterator referring to the past-the-end element in the multiset container.
  • (1) iterator erase (const_iterator position);
    (2) size_type erase (const value_type& val);
    (3) iterator erase (const_iterator first, const_iterator last);
    Removes elements from the multiset container.
  • const_iterator find (const value_type& val) const;
    iterator find (const value_type& val);
    Searches the container for an element equivalent to val and returns an iterator to it if found, otherwise it returns an iterator to multiset::end.
  • const_iterator lower_bound (const value_type& val) const;
    iterator lower_bound (const value_type& val);
    Returns an iterator pointing to the first element in the container which is not considered to go before val (i.e., either it is equivalent or goes after).
  • size_type size() const noexcept;
    Returns the number of elements in the multiset container.
  • const_iterator upper_bound (const value_type& val) const;
    iterator upper_bound (const value_type& val);
    Returns an iterator pointing to the first element in the container which is considered to go after val.

 

Example

#include <iostream>
#include <set>
#include <iterator>
 
using namespace std;
 
int main()
{
  // Declaring a multiset
  multiset <int> mset; 
  multiset <int> ::iterator it;
  
  // Inserting elements
  for (int i=0; i<5; i++) {
    mset.insert (i+1); 
  }
  
  // 5 is again inserted. This is allowed here unlike set
  mset.insert (5);
  
  // Showing elements in multiset
  for(it= mset.begin(); it!= mset.end(); it++) {
    cout << *it;
  }
 
  // Size of multiset
  cout << mset.size();
  
  bool chk = mset.empty();
  if (chk==1) {
    cout << "Multiset is empty";
  }
  else{
    cout << "Multiset is not empty";
  }
  
  // Number of times element 2 occured
  cout << mset.count(2);
  
  // Searching element 6
  it = mset1.find(3);
  cout << "Element found is " << *it;
  
  // Assigning elements of the mset to other multiset
  multiset <int> mset2(mset.begin(), mset.end());
  
 
  // Lower bound and upper bound 
  multiset <int> :: iterator lob, upb;
  lob = mset1.lower_bound (2);
  upb = mset1.upper_bound (4);
 
  // Erase operation
  mset.erase(5);
  
  // Applying clear operation
  mset.clear();
  
  return 0;
}