Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

C++ STL: multimap (Complete Guide)

Divya Jain
Divya Jain

Multimaps are containers in STL that are very similar to maps, they store the elements as key-value pairs. Unlike maps, multimaps can have duplicate keys. The elements are stored in sorted order based on their key value. Keys cannot be changed, once they are added to the map, they can only be inserted or deleted, but the values can be altered. 

They are implemented as Red-Black Trees in memory. The multimap is present in #include<map> header file. The elements inside the multimap can be accessed using iterators. 

Multimap Declaration

Syntax:

multimapmap <key_data_type, value_data_type> name {initial_values};

key_data_type: data type of the key to be stored inside the map

value_data_type: data type of the value to be stored inside the map

initial_values: optional parameter which initializes the map with the given values

Note: By default the map stores values in non-decreasing order sorted according to key. To store the values in non-increasing order, we use an inbuilt comparator function

multimap <key_data_type, value_data_type, greater <data_type>> name;

Example:

multimap <int, string> m; //initializes a multimap of size 0 which have key elements as integers and values as strings arranged in non-decreasing order based on keys
multimap <int, string> m = {{1, "a"}, {1, "b"}, {2, "ab"}, {3, "abc"}} //initializes a multimap having initial key-value pairs as {1, a}, {1, b}, {2, ab}, {3, abc}
multimap <int, string, greater <int>> m; //initializes a multimap of size 0 which which have key elements as integers and values as strings arranged in non-increasing order

Functions on maps

  • begin(): Returns an iterator to the first element of the multimap.
    Parameters: None
    Return type: iterator
     
  • end(): Returns an iterator to the element past the last element of the multimap.
    Parameters: None
    Return type: iterator
     
  • size(): It tells us the size of the multimap.
    Parameters: None
    Return type: integer - total number of elements in the multimap
     
  • insert(pair): Insert a pair in the multimap.
    Parameters: the pair to be inserted
    Return type: void
     
  • erase(key_val) or erase(pos_iterator): Delete all elements from the multimap with key_val as the key value or delete the element at the specified position.
    Parameters: the key of the element to be removed or iterator pointing to the position at which the element needs to be deleted
    Return type: integer, number of deleted values
     
  • find(key): Returns an iterator pointing to the element, if the key is found else returns an iterator pointing to the end of the multimap. If there are duplicate keys, returns an iterator to the first one.
    Parameters: the element which needs to be found
    Return type: iterator
     
  • clear(): It deletes all the elements from the multimap
    Parameters: None
    Return type: void
     
  • empty(): It tells us whether the multimap is empty or not.
    Parameters: None
    Return type: Boolean, true if a multimap is empty else false
#include<iostream>
#include<map>

using namespace std;

int main() {
  multimap <int, string> m1;
  m1.insert(pair <int, string> (1, "a"));
  m1.insert(pair <int, string> (1, "b"));
  m1.insert(pair <int, string> (2, "ab"));
  m1.insert(pair <int, string> (3, "abc"));
  m1.insert(pair <int, string> (4, "abcd"));
  multimap <int, string, greater <int>> m2;
  m2.insert(pair <int, string> (1, "a"));
  m2.insert(pair <int, string> (1, "b"));
  m2.insert(pair <int, string> (2, "ab"));
  m2.insert(pair <int, string> (3, "abc"));
  m2.insert(pair <int, string> (4, "abcd"));

  multimap <int, string> ::iterator it;

  cout << "Multimap1\n";
  for (it = m1.begin(); it != m1.end(); it++)
    cout << it -> first << " " << it -> second << '\n';
  cout << '\n';
  cout << "Multimap2\n";
  for (it = m2.begin(); it != m2.end(); it++)
    cout << it -> first << " " << it -> second << '\n';
  cout << '\n';

  m1.erase(1); // delete all occurrences of 1 
  m2.erase(m2.find(1)); // Finds returns an iterator, so only value will be deleted i.e value at specified position
  cout << "After erasing element, size of multimap1 is " << m1.size() << '\n';
  cout << "After erasing element, size of multimap2 is " << m2.size() << '\n';
  int val = 3;
  if (m1.find(val) != m1.end())
    cout << "The multimap1 contains " << val << " as key" << endl;
  else
    cout << "The multimap1 does not contains " << val << " as key" << endl;
  cout << "Elements of multimap1\n";
  for (it = m1.begin(); it != m1.end(); it++)
    cout << it -> first << " " << it -> second << '\n';
  cout << '\n';
  cout << "Elements of multimap2\n";
  for (it = m2.begin(); it != m2.end(); it++)
    cout << it -> first << " " << it -> second << '\n';
  cout << '\n';

  m1.clear();
  if (m1.empty() == true) {
    cout << "Multimap1 is empty now!";
  }
  return 0;
}

Output

Multimap1
1 a
1 b
2 ab
3 abc
4 abcd

Multimap2
4 abcd
3 abc
2 ab
1 a
1 b

After erasing element, size of multimap1 is 3
After erasing element, size of multimap2 is 4
The multimap1 contains 3 as key
Elements of multimap1
2 ab
3 abc
4 abcd

Elements of multimap2
4 abcd
3 abc
2 ab
1 b

Multimap1 is empty now!
Divya Jain
Divya Jain
Divya is an incoming SDE at Atlassian. She loves solving problems and web development. She loves to explore new things and is up for interesting conversations about tech.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet