-
Hajipur, Bihar, 844101
In C++, a map is an associative container that stores elements in key-value pairs. Each key in a map is unique and can be used to access its associated value. Maps automatically arrange their elements in ascending order of keys, making them useful when you need fast lookups, sorted data, or unique key-value associations.
Maps are implemented as balanced binary search trees (Red-Black Trees), which ensures that operations such as insertion, deletion, and search take O(log n) time on average.
Stores key-value pairs (like a dictionary in Python).
Keys are unique, and duplicate keys are not allowed.
Automatically sorts the elements by key in ascending order.
Each key can be mapped to only one value.
Efficient searching, insertion, and deletion operations in O(log n) time.
To use a map, include the <map> header and declare it like this:
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> students;
students[1] = "Anita";
students[2] = "Pooja";
students[3] = "Kavya";
for (auto s : students)
cout << s.first << " => " << s.second << endl;
return 0;
}
Output:
1 => Anita
2 => Pooja
3 => Kavya
Here, the numbers (1, 2, 3) are keys, and the names are values.
| Feature | map | multimap |
|---|---|---|
| Key uniqueness | Keys must be unique | Duplicate keys allowed |
| Element modification | Value can be changed | Not directly modifiable |
| Typical use case | Unique identifiers | Storing grouped values for the same key |
Example of multimap:
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<int, string> marks;
marks.insert({1, "A"});
marks.insert({1, "B"}); // Duplicate key
for (auto m : marks)
cout << m.first << " => " << m.second << endl;
return 0;
}
Output:
1 => A
1 => B
| Function | Description |
|---|---|
insert({key, value}) |
Inserts a new element |
erase(key) |
Removes the element with the given key |
find(key) |
Returns iterator to the element if found |
count(key) |
Checks if a key exists (returns 1 or 0) |
size() |
Returns number of key-value pairs |
clear() |
Removes all elements from map |
empty() |
Checks if the map is empty |
begin() / end() |
Returns iterator to start/end of map |
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> city;
city.insert({1, "Delhi"});
city.insert({2, "Mumbai"});
city.insert({3, "Kolkata"});
cout << "Cities:\n";
for (auto c : city)
cout << c.first << " -> " << c.second << endl;
return 0;
}
Output:
Cities:
1 -> Delhi
2 -> Mumbai
3 -> Kolkata
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> ages;
ages["Anita"] = 25;
ages["Kavya"] = 22;
ages["Anita"] = 26; // Update value
for (auto a : ages)
cout << a.first << " is " << a.second << " years old." << endl;
return 0;
}
Output:
Anita is 26 years old.
Kavya is 22 years old.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> students = {{1, "Anita"}, {2, "Pooja"}, {3, "Kavya"}};
int roll = 2;
auto it = students.find(roll);
if (it != students.end()) {
cout << "Found: " << it->first << " => " << it->second << endl;
students.erase(roll);
} else {
cout << "Student not found.";
}
cout << "\nAfter deletion:\n";
for (auto s : students)
cout << s.first << " => " << s.second << endl;
return 0;
}
Output:
Found: 2 => Pooja
After deletion:
1 => Anita
3 => Kavya
for (map<int, string>::iterator it = students.begin(); it != students.end(); ++it) {
cout << it->first << " => " << it->second << endl;
}
This approach gives more control, especially when modifying or erasing elements while iterating.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> fruits = {{1, "Apple"}, {2, "Banana"}};
cout << "Is key 2 present? " << fruits.count(2) << endl;
cout << "Is key 5 present? " << fruits.count(5) << endl;
return 0;
}
Output:
Is key 2 present? 1
Is key 5 present? 0
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> data = {{1, "A"}, {2, "B"}, {3, "C"}};
cout << "Size before clear: " << data.size() << endl;
data.clear();
cout << "Size after clear: " << data.size() << endl;
cout << "Is map empty? " << (data.empty() ? "Yes" : "No");
return 0;
}
Output:
Size before clear: 3
Size after clear: 0
Is map empty? Yes
C++ maps are one of the most useful containers in the STL, allowing you to store unique keys and associated values efficiently. They keep elements sorted automatically and provide fast lookup and insertion times. For cases where duplicate keys are needed, a multimap is available.
Maps are widely used in real-world applications like maintaining user databases, storing configuration settings, counting frequencies, and building dictionaries.
Write a C++ program to create a map of roll numbers and student names, and display all entries.
Create a program that takes city names and their population as key-value pairs and displays them in sorted order by city name.
Write a program to find and delete a key-value pair from a map using the find() and erase() functions.
Build a C++ program that counts how many times each word appears in a given string using a map.
Write a program to check if a particular key exists in a map using count().
Create a C++ program that swaps two maps and displays their contents before and after swapping.
Write a program to demonstrate how duplicate keys are handled differently in map and multimap.
Build a program that stores product IDs and prices in a map and allows the user to update a price by entering the product ID.
Write a C++ program to find the key with the highest value in a map of integers.
Create a program that sorts a map of student names and marks, and prints only those who scored above 80.