codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

69 / 87 topics
62Introduction to STL & Containers63std::array64Vectors65List & Forward List66Deque67Stack68Queue & Priority Queue69Map & Multimap70Set & Multiset71Unordered Map & Unordered Multimap72Unordered Set & Unordered Multiset73Iterators74Algorithms75Functors
Tutorials/C++ Programming/Map & Multimap
⚡C++ Programming

Map & Multimap

Updated 2026-05-12
30 min read

Map & Multimap

In the world of programming, managing data efficiently is crucial. C++ Standard Template Library (STL) provides several containers that help in organizing and manipulating data effectively. Two such powerful containers are std::map and std::multimap. These containers store elements as key-value pairs, where each key is unique for std::map, but can be duplicated in std::multimap.

Introduction

Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order. The key serves as an index to access the corresponding mapped value. Maps automatically sort their elements based on the keys.

Multimaps, on the other hand, allow duplicate keys but maintain the same ordering criteria for the elements. This makes multimaps useful when you need to store multiple entries with the same key.

Understanding and effectively using std::map and std::multimap can significantly enhance your ability to manage data in C++ applications.

Core Content

Key-Value Pairs

Both std::map and std::multimap store elements as pairs of keys and values. The key is used for sorting and accessing the elements, while the value holds the actual data associated with that key.

Example: Basic Map Usage

C++
1#include <iostream>
2#include <map>
3
4int main() {
5 std::map<int, std::string> myMap;
6 myMap[1] = "One";
7 myMap[2] = "Two";
8 myMap[3] = "Three";
9
10 for (const auto& pair : myMap) {
11 std::cout << pair.first << ": " << pair.second << std::endl;
12 }
13
14 return 0;
15}
Output

Retrieval

To retrieve an element from a map, you can use the find() method or directly access it using the key.

Example: Using find()

C++
1#include <iostream>
2#include <map>
3
4int main() {
5 std::map<int, std::string> myMap;
6 myMap[1] = "One";
7 myMap[2] = "Two";
8 myMap[3] = "Three";
9
10 auto search = myMap.find(2);
11 if (search != myMap.end()) {
12 std::cout << "Found: " << search->first << ": " << search->second << std::endl;
13 } else {
14 std::cout << "Not found" << std::endl;
15 }
16
17 return 0;
18}
Output

Iterating

Maps provide iterators to traverse the elements in a sorted order.

Example: Iterating Over a Map

C++
1#include <iostream>
2#include <map>
3
4int main() {
5 std::map<int, std::string> myMap;
6 myMap[1] = "One";
7 myMap[2] = "Two";
8 myMap[3] = "Three";
9
10 for (auto it = myMap.begin(); it != myMap.end(); ++it) {
11 std::cout << it->first << ": " << it->second << std::endl;
12 }
13
14 return 0;
15}
Output

Operator[]

The operator[] can be used to access and modify the value associated with a key. If the key does not exist, it is inserted with a default-constructed value.

Example: Using operator[]

C++
1#include <iostream>
2#include <map>
3
4int main() {
5 std::map<int, std::string> myMap;
6 myMap[1] = "One";
7 myMap[2] = "Two";
8 myMap[3] = "Three";
9
10 std::cout << "Element at key 2: " << myMap[2] << std::endl;
11
12 // Accessing a non-existent key
13 std::cout << "Element at key 4 (default constructed): " << myMap[4] << std::endl;
14 std::cout << "Size after accessing key 4: " << myMap.size() << std::endl;
15
16 return 0;
17}
Output

Summary

ConceptDescription
Key-Value PairsMaps store elements as key-value pairs where keys are unique.
InsertionElements can be inserted using insert() or by direct assignment.
RetrievalUse find() to locate an element, or access it directly via the key.
DeletionUse erase() to remove elements from a map.
IterationMaps provide iterators for traversing elements in sorted order.
MultimapAllows duplicate keys and maintains ordering.
Operator[]Accesses or modifies the value associated with a key, inserting if necessary.

What's Next?

In the next section, we will explore std::set and std::multiset, which are containers that store unique elements in sorted order. These containers are useful when you need to manage collections of items without duplicates and require efficient search operations.

Stay tuned for more insights into C++ STL!


PreviousQueue & Priority QueueNext Set & Multiset

Recommended Gear

Queue & Priority QueueSet & Multiset