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: unordered_multimap (Complete Guide)

Divya Jain
Divya Jain

Unordered_multimaps are containers in STL that are very similar to unordered_map except for the fact that they store duplicate keys. The elements inside unordered_multimaps are stored as key-value pairs in any order.  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 hash-table in memory. When a duplicated key-value pair is inserted into the map, the count of the key-value pair is incremented. The map is present in #include<unordered_map> header file. The elements inside the map can be accessed using iterators. 

Unordered_multimap Declaration

Syntax:

unordered_multimap <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

Example:

unordered_multimap <int, string> m; //initializes a unordered_multimap of size 0 which have key elements as integers and values as strings 
unordered_multimap <int, string> m = {{1, "a"}, {2, "b"}, {2, "ab"}, {2, "ab"}, {3, "abc"} //initializes a unordered_multimap having initial key-value pairs as {1, a}, {2, b}, {2, ab}, {3, abc}

Functions on unordered_multimaps

  • begin(): Returns an iterator to the first element of the unordered_multimap.
    Parameters: None
    Return type: iterator
     
  • end(): Returns an iterator to the element past the last element of the unordered_multimap.
    Parameters: None
    Return type: iterator
     
  • size(): It tells us the size of the unordered_multimap.
    Parameters: None
    Return type: integer - total number of elements in the unordered_multimap
     
  • insert(pair): Insert a pair in the unordered_multimap.
    Parameters: the pair to be inserted
    Return type: void
     
  • erase(value) or erase(start_iterator,end_iterator): Delete elements from the unordered_multimap.
    Parameters: the value whose occurrences has to be removed or iterators pointing to the position between which the value needs to be deleted
    Return type: The first one returns the number of elements erased and the second one returns an iterator immediately following the last element erased. 
     
  • find(key): Returns an iterator pointing to the element, if the key is found else returns an iterator pointing to the end of the unordered_multimap.
    Parameters: the element which needs to be found
    Return type: iterator
     
  • bucket_count(): It tells us the total number of buckets in the unordered_multimap.
    Parameters: None
    Return type: integer - total number of buckets in the unordered_multimap
     
  • count(key): It tells us the total number of occurrences of the key in the unordered_multimap.
    Parameters: key whose occurrences needs to be found
    Return type: integer - total number of occurrences of the key in the unordered_multimap
     
  • clear(): It deletes all the elements from the unordered_multimap
    Parameters: None
    Return type: void
     
  • empty(): It tells us whether the unordered_multimap is empty or not.
    Parameters: None
    Return type: Boolean, true if a unordered_multimap is empty else false
#include<iostream>
#include<unordered_map>

using namespace std;

int main() {
  unordered_multimap <int, string> m;
  m.insert(pair <int, string> (1, "a"));
  m.insert(pair <int, string> (2, "ab"));
  m.insert(pair <int, string> (2, "b"));
  m.insert(pair <int, string> (3, "abc"));
  m.insert(pair <int, string> (4, "abcd"));

  unordered_multimap <int, string> ::iterator it;

  cout << "Unordered_multimap contains\n";
  for (it = m.begin(); it != m.end(); it++)
    cout << it -> first << " " << it -> second << '\n';
  cout << '\n';

  m.erase(1);
  m.erase(m.find(2));
  cout << "After erasing element, size of unordered_multimap is " << m.size() << '\n';
  int val = 3;
  if (m.find(val) != m.end())
    cout << "The unordered_multimap contains " << val << " as key" << endl;
  else
    cout << "The unordered_multimap does not contains " << val << " as key" << endl;
  cout << "Elements of unordered_multimap\n";
  for (it = m.begin(); it != m.end(); it++)
    cout << it -> first << " " << it -> second << '\n';
  cout << '\n';
  cout << "No of occurrences of 5 in unordered_multimap is " << m.count(5) << "\n";
  int n = m.bucket_count();

  cout << "map has " << n << " buckets.\n";

  for (int i = 0; i < n; ++i) {
    cout << "bucket " << i << " contains:";
    for (auto it = m.begin(i); it != m.end(i); ++it)
      cout << " " << it -> first << " " << it -> second;
    cout << "\n";
  }

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

Output

Unordered_multimap contains
4 abcd
3 abc
1 a
2 b
2 ab

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

No of occurrences of 5 in unordered_multimap is 0
map has 5 buckets.
bucket 0 contains:
bucket 1 contains:
bucket 2 contains: 2 ab
bucket 3 contains: 3 abc
bucket 4 contains: 4 abcd
Unordered_multimap 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