std::map is an associative container in the C++ Standard Template Library (STL) that stores elements in key-value pairs. It maintains the keys in sorted order using a binary search tree (typically a red-black tree).
Key Features:
- Key-Value Pair: Each element is a combination of a unique key and a corresponding value.
- Sorted Order: Keys are always sorted in ascending order by default.
- Unique Keys: Each key must be unique.
- Efficient Operations:
- Insertion, deletion, and search have O(log n) complexity.
- Custom Comparator: You can define a custom comparator for sorting keys.
Basic Syntax
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
map<string, int> ageMap;
// Insert elements
ageMap["Alice"] = 25;
ageMap["Bob"] = 30;
ageMap["Charlie"] = 20;
// Iterate and print
for (const auto &pair : ageMap) {
cout << pair.first << ": " << pair.second << endl;
}
return 0;
}
Output:
Alice: 25
Bob: 30
Charlie: 20
Example 1: Mapping Strings to Integers
Problem: Store the frequency of words in a sentence.
#include <iostream>
#include <map>
#include <sstream>
using namespace std;
int main() {
string sentence = "this is a test this is only a test";
map<string, int> wordFrequency;
stringstream ss(sentence);
string word;
while (ss >> word) {
wordFrequency[word]++;
}
// Print word frequencies
for (const auto &pair : wordFrequency) {
cout << pair.first << ": " << pair.second << endl;
}
return 0;
}
Output:
a: 2
is: 2
only: 1
test: 2
this: 2
Example 2: Mapping Integers to Strings
Problem: Map student IDs to their names.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> studentMap;
// Insert student records
studentMap[101] = "Alice";
studentMap[102] = "Bob";
studentMap[103] = "Charlie";
// Access by key
cout << "Student with ID 102: " << studentMap[102] << endl;
// Print all records
for (const auto &pair : studentMap) {
cout << "ID: " << pair.first << ", Name: " << pair.second << endl;
}
return 0;
}
Output:
Student with ID 102: Bob
ID: 101, Name: Alice
ID: 102, Name: Bob
ID: 103, Name: Charlie
Example 3: Mapping Characters to Integers
Problem: Count the frequency of characters in a string.
#include <iostream>
#include <map>
using namespace std;
int main() {
string text = "hello world";
map<char, int> charFrequency;
for (char c : text) {
if (c != ' ') { // Skip spaces
charFrequency[c]++;
}
}
// Print character frequencies
for (const auto &pair : charFrequency) {
cout << pair.first << ": " << pair.second << endl;
}
return 0;
}
Output:
: 1
e: 1
h: 1
l: 3
o: 2
r: 1
w: 1
Example 4: Mapping Pairs
Problem: Map pairs of integers to strings.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<pair<int, int>, string> pairMap;
// Insert pairs
pairMap[{1, 2}] = "Alice and Bob";
pairMap[{2, 3}] = "Charlie and Dave";
// Access by pair
cout << "Pair (1, 2): " << pairMap[{1, 2}] << endl;
// Print all pairs
for (const auto &pair : pairMap) {
cout << "(" << pair.first.first << ", " << pair.first.second << "): " << pair.second << endl;
}
return 0;
}
Output:
Pair (1, 2): Alice and Bob
(1, 2): Alice and Bob
(2, 3): Charlie and Dave
Example 5: Custom Comparator
Problem: Sort keys in descending order.
#include <iostream>
#include <map>
using namespace std;
// Custom comparator
struct Descending {
bool operator()(int a, int b) const {
return a > b;
}
};
int main() {
map<int, string, Descending> customMap;
// Insert elements
customMap[1] = "One";
customMap[3] = "Three";
customMap[2] = "Two";
// Print elements
for (const auto &pair : customMap) {
cout << pair.first << ": " << pair.second << endl;
}
return 0;
}
Output:
3: Three
2: Two
1: One
Example 6: Mapping Multitypes
Problem: Map a string to a vector of integers.
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
map<string, vector<int>> studentGrades;
// Insert grades
studentGrades["Alice"] = {90, 85, 88};
studentGrades["Bob"] = {75, 80, 72};
// Print grades
for (const auto &pair : studentGrades) {
cout << pair.first << ": ";
for (int grade : pair.second) {
cout << grade << " ";
}
cout << endl;
}
return 0;
}
Output:
Alice: 90 85 88
Bob: 75 80 72
Key Methods in std::map
insert: Add key-value pairs.find: Search for a key.erase: Remove a key-value pair.size: Return the number of elements.clear: Remove all elements.count: Check if a key exists.begin,end: Iterators for traversal.
