Map (Key-Value Pairs)
C++
Medium
3 views
Problem Description
Store data in key-value pairs like dictionary.
Real Life: Phone book - name (key) to number (value).
Step-by-Step Logic:
1. Create map
2. Insert key-value pairs
3. Access value using key
4. Check if key exists
5. Iterate through map
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!
No comments yet. Start the discussion!