Map Basics
Basic Map Operations in C++
C++ Map Basics Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
// Create map
map<string, int> studentMarks;
// Insert elements
studentMarks["Alice"] = 95;
studentMarks["Bob"] = 87;
studentMarks["Charlie"] = 92;
studentMarks["David"] = 78;
// Display map
cout << "Student Marks:" << endl;
for (const auto& pair : studentMarks) {
cout << pair.first << ": " << pair.second << endl;
}
// Access elements
cout << "\nAlice's marks: " << studentMarks["Alice"] << endl;
cout << "Bob's marks: " << studentMarks.at("Bob") << endl;
// Check if key exists
if (studentMarks.find("Eve") != studentMarks.end()) {
cout << "Eve found" << endl;
} else {
cout << "Eve not found" << endl;
}
// Count occurrences (0 or 1 for map)
cout << "Number of students named Alice: " << studentMarks.count("Alice") << endl;
// Size
cout << "\nTotal students: " << studentMarks.size() << endl;
// Erase element
studentMarks.erase("David");
cout << "\nAfter removing David:" << endl;
for (const auto& pair : studentMarks) {
cout << pair.first << ": " << pair.second << endl;
}
return 0;
}Student Marks: Alice: 95 Bob: 87 Charlie: 92 David: 78 Alice's marks: 95 Bob's marks: 87 Eve not found Number of students named Alice: 1 Total students: 4 After removing David: Alice: 95 Bob: 87 Charlie: 92
Understanding Map Basics
This program teaches you how to use Map Basics in C++. Map is an associative container from the STL that stores key-value pairs. Keys are unique and automatically sorted, making maps perfect for dictionary-like data structures where you need to associate values with keys.
---
1. What This Program Does
The program demonstrates basic map operations:
Maps provide efficient key-value storage and retrieval.
---
2. Header Files Used
---
3. Understanding Maps
Map Concept
:
Key Features
:
---
4. Creating Maps
Declaration
:
map<string, int> studentMarks;
How it works
:
---
5. Inserting Elements
Using [] Operator
:
studentMarks["Alice"] = 95;
studentMarks["Bob"] = 87;
Using insert()
:
studentMarks.insert({"Charlie", 92});
How it works
:
---
6. Accessing Values
Using [] Operator
:
int marks = studentMarks["Alice"];
Using at()
:
int marks = studentMarks.at("Bob");
Difference
:
---
7. Finding Elements
Using find()
:
auto it = studentMarks.find("Eve");
if (it != studentMarks.end()) {
} else {
// Key not found
}
// Key foundHow it works
:
---
8. Counting Occurrences
Using count()
:
int count = studentMarks.count("Alice");
How it works
:
---
9. Erasing Elements
Using erase()
:
studentMarks.erase("David");
How it works
:
---
10. Iterating Through Map
Range-based Loop
:
}
cout << pair.first << ": " << pair.second << endl;How it works
:
---
11. When to Use Maps
Best For
:
Example Scenarios
:
---
12. Important Considerations
Automatic Sorting
:
Performance
:
Key Uniqueness
:
---
13. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning associative containers, understanding key-value storage, and preparing for advanced data structures and algorithms in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Map Basics
This C++ program is part of the "STL Containers Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.