C++ Program to Map (Key-Value Pairs) with Explanation
C++
Medium
STL
36 views
1 min read
198 words
This problem helps you practice core C++ fundamentals in a practical way. It builds intuition around pair, key, key-value. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Store data in key-value pairs like dictionary.
Real Life: Phone book - name (key) to number (value).
Input Format
An integer n (number of entries)
Then n pairs:
key value
Output Format
All key–value pairs printed.
Constraints
• Keys must be unique
• Values can repeat
• Stored in sorted order by key
Concept Explanation
A map stores data in key–value pairs, just like a dictionary.
Each key is linked to exactly one value.
Example:
• Phone book → Name (key) → Phone number (value)
Step-by-Step Logic
1.Create a map.
2.Take input key and value.
3.Insert pair into map using map[key] = value.
4.If key already exists:
• Old value is replaced by new value.
5.Map automatically stores data sorted by key.
6.Traverse the map using loop.
7.Print each key with its value.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
void stl_q6_map() {
map<string, int> ages;
// Insert key-value pairs
ages["Raj"] = 20;
ages["Priya"] = 22;
ages["Amit"] = 21;
cout << "Raj's age: " << ages["Raj"] << endl;
// Iterate
cout << "All ages:" << endl;
for(auto pair : ages) {
cout << pair.first << " : " << pair.second << endl;
}
}
Output Example
Input:
3
Rahul 9876
Amit 9123
Neha 9988
Output:
Amit 9123
Neha 9988
Rahul 9876
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Store data in key-value pairs like dictionary.
Real Life: Phone book - name (key) to number (value).
Input / Output
Input
An integer n (number of entries)
Then n pairs:
key value
Output
All key–value pairs printed.
Constraints
• Keys must be unique
• Values can repeat
• Stored in sorted order by key
Examples
Input:
3
Rahul 9876
Amit 9123
Neha 9988
Output:
Amit 9123
Neha 9988
Rahul 9876
Explanation
Concept Explanation
A map stores data in key–value pairs, just like a dictionary.
Each key is linked to exactly one value.
Example:
• Phone book → Name (key) → Phone number (value)
Step-by-Step Explanation
1.Create a map.
2.Take input key and value.
3.Insert pair into map using map[key] = value.
4.If key already exists:
• Old value is replaced by new value.
5.Map automatically stores data sorted by key.
6.Traverse the map using loop.
7.Print each key with its value.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
void stl_q6_map() {
map<string, int> ages;
// Insert key-value pairs
ages["Raj"] = 20;
ages["Priya"] = 22;
ages["Amit"] = 21;
cout << "Raj's age: " << ages["Raj"] << endl;
// Iterate
cout << "All ages:" << endl;
for(auto pair : ages) {
cout << pair.first << " : " << pair.second << endl;
}
}
Solutions (0)
No solutions submitted yet. Be the first!