Multi-map is an associative container like map. It internally store elements in key value pair. But unlike map which store only unique keys, multimap can have duplicate keys. Also, it internally keep elements in sorted order of keys. multimap containers are generally slower than unordered_multimap containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.

Important functions

  • iterator begin() noexcept;
    const_iterator begin() const noexcept;
    Returns an iterator referring to the first element in the multimap container.
  • void clear() noexcept;
    Removes all elements from the multimap container (which are destroyed), leaving the container with a size of 0.
  • size_type count (const key_type& k) const;
    Searches the container for elements with a key equivalent to k and returns the number of matches.
  • iterator end() noexcept;
    const_iterator end() const noexcept;
    Returns an iterator referring to the past-the-end element in the multimap 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 elements from the multimap container.
  • iterator find (const key_type& k);
    const_iterator find (const key_type& k) const;
    Searches the container for an element with a key equivalent to k and returns an iterator to it if found, otherwise it returns an iterator to multimap::end.
  • size_type size() const noexcept;
    Returns the number of elements in the multimap container.
  • value_compare value_comp() const;
    Returns a comparison object that can be used to compare two elements to get whether the key of the first one goes before the second.



#include <iostream> 
#include <map> 
#include <iterator> 

using namespace std; 

int main() 
  // Empty multimap container 
  multimap <int, int> map;

  // insert elements in random order 
  map.insert(pair <int, int> (1, 40)); 
  map.insert(pair <int, int> (2, 30)); 
  map.insert(pair <int, int> (3, 60)); 
  map.insert(pair <int, int> (4, 20)); 
  map.insert(pair <int, int> (5, 50)); 
  map.insert(pair <int, int> (6, 50)); 
  map.insert(pair <int, int> (6, 10)); 

  // Printing multimap map 
  multimap <int, int> :: iterator itr; 
  for (itr = map.begin(); itr != map.end(); ++itr) 
    cout << itr->first << itr->second << '\n'; 

  // Assigning the elements from map
  multimap <int, int> map2(map.begin(),map.end()); 

  // Remove all elements up to element with value 3 
  map2.erase(map2.begin(), map2.find(3));

  // Elements with key 6
  cout << map.count(6);

  // Remove all elements with key = 4 
  int keyValue; 
  keyValue = map2.erase(4);  

  // Lower bound and upper bound for multimap map key = 5 
  cout << map.lower_bound(5)->first; 
  cout << map.upper_bound(5)->first; 

  itr = map.find(5);
  // Erasing by iterator
  map.erase (itr);                     

  // Erasing by key (2 elements)
  map.erase (6);                    
  // Clear the map
  return 0;