C++ STL Tutorial - Map
Map in C++ STL is an associative container that stores elements in key-value pairs. The key is unique and the value can be accessed using the key. Map is implemented as a balanced tree, which provides O(log n) complexity for inserting, searching and deleting operations.
Syntax
std::map<key_type, value_type> map_name;
Examples:
std::map<int, std::string> student_map; // Key: int, value: string
std::map<std::string, float> score_map; // Key: string, value: float
Example
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> student_map;
student_map[1111] = "John";
student_map[2222] = "Alice";
student_map[3333] = "Bob";
std::cout << "Map size: " << student_map.size() << std::endl;
std::cout << "Student with roll no. 2222: " << student_map[2222] << std::endl;
return 0;
}
Output
Map size: 3
Student with roll no. 2222: Alice
Explanation
In the above example, we have defined a map called "student_map" with integer keys and string values. We have added three key-value pairs to the map using the syntax map_name[key] = value
.
We can access the value of a key by using map_name[key]
. The size()
function returns the number of key-value pairs in the map.
Use
Maps are useful for storing and accessing data in key-value form, where keys and values can be of different types. They are particularly useful when performing lookups, as searches can be done in O(log n) time.
#include <iostream>
#include <map>
int main() {
std::map<std::string, float> score_map;
score_map["John"] = 75.5;
score_map["Alice"] = 80.0;
score_map["Bob"] = 85.5;
std::string name;
std::cout << "Enter a student name: ";
std::cin >> name;
if (score_map.find(name) != score_map.end()) {
std::cout << name << "'s score is " << score_map[name] << std::endl;
} else {
std::cout << "No record found for " << name << std::endl;
}
return 0;
}
In the above example, we have defined a map called "score_map" with string keys and float values. We have added three key-value pairs to the map using the syntax map_name[key] = value
.
We then ask the user to enter a student name, and check if the name exists in the map using the find()
function. If the key is found, we print the corresponding value. Otherwise, we print a message saying that no record was found.
Important Points
- Map is a part of the C++ Standard Template Library (STL).
- It is an associative container that stores elements in key-value pairs.
- Key is unique and the value can be accessed using the key.
- It is implemented as a balanced tree, which provides O(log n) complexity for inserting, searching and deleting operations.
- Maps are useful for storing and accessing data in key-value form, where keys and values can be of different types.
Summary
In summary, map in C++ STL is an associative container that stores elements in key-value pairs. It provides efficient ways to insert, search and delete elements using the key. Maps are useful for storing and accessing data in key-value form, where keys and values can be of different types, and are particularly useful for performing lookups.