
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Multimap in STL
A multimap is an associative container that stores elements in a key-value pair format, where multiple values can share the same key. In this article, we will learn how to use a multimap from the Standard Template Library (STL) in C++.
What is Multimap?
Multimap is a sorted associative container that stores data in key-value pairs, where keys can occur multiple times. Keys are stored in sorted order, and you can insert, access, and remove elements based on the key.
For example, in a multimap we can store student names with their scores even if some students have the same score:
multimap<int, string> scores; scores.insert({90, "Alice"}); scores.insert({85, "Bob"}); scores.insert({90, "Charlie"}); // Display all entries with the same score auto range = scores.equal_range(90); for (auto it = range.first; it != range.second; ++it) { cout << "Score: " << it->first << ", Name: " << it->second << endl; } //Output will be displayed as: Score: 90, Name: Alice Score: 90, Name: Charlie
Using multimap Class in STL
The <multimap> class is part of the C++ standard STL library. It stores key-value pairs in sorted order and allows duplicate keys. Below are some functions used in multimap class:
- insert(): Adds a new key-value pair into the multimap, including duplicates.
- erase(): Removes elements with the specified key or at a specific position.
- find(): Searches for the first occurrence of a key and returns an iterator to it.
- count(): Returns the number of elements with the specified key.
- equal_range(): Returns the range of elements with the specified key.
- clear(): Removes all elements from the multimap.
Steps to Implement Multimap in C++ STL
Following are steps/algorithm to implement a multimap using C++ STL:
- Create a multimap using std::multimap.
- Insert key-value pairs using insert().
- Access duplicate keys using equal_range().
- Remove elements using erase().
- Iterate and display multimap elements using a loop.
C++ Program to Implement Multimap using STL
The below code is implemention of the above algorithm in C++ language.
#include <iostream> #include <map> #include <string> using namespace std; int main() { multimap<int, string> students; // Insert elements with duplicate keys students.insert({101, "Rahul"}); students.insert({102, "Priya"}); students.insert({101, "Amit"}); // Display all elements cout << "Student Records:" << endl; for (auto& entry : students) { cout << "Roll No: " << entry.first << ", Name: " << entry.second << endl; } // Display entries with roll number 101 cout << "Students with Roll No 101:" << endl; auto range = students.equal_range(101); for (auto it = range.first; it != range.second; ++it) { cout << "Name: " << it->second << endl; } return 0; }
The output of above code will be
Student Records: Roll No: 101, Name: Rahul Roll No: 101, Name: Amit Roll No: 102, Name: Priya Students with Roll No 101: Name: Rahul Name: Amit
Time and Space Complexity
- Insertion, Deletion, and Search: O(log n) for each operation, using a balanced binary search tree.
- Iteration: O(n) as we visit each element once.
Space Complexity: O(n) where n is the number of key-value pairs in the multimap.