“Know how to solve every problem that has been solved.” “What I cannot create, I do not understand.” — Richard Feynman

Map

Programming

Audited·on 2026-05-14·short read

This program demonstrates std::map, an associative container — a container you look things up in by key rather than by position. A vector is positional: you ask for element 7. A map is associative: you ask for the value stored under "alice". std::map keeps its key–value pairs in sorted key order.

Implementation

#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> ageMap;
    ageMap["Alice"] = 25;
    ageMap["Bob"] = 30;

    for (const auto& pair : ageMap) {
        std::cout << pair.first << ": " << pair.second << " years old" << std::endl;
    }
    return 0;
}

Key Concepts

Output

Alice: 25 years old
Bob: 30 years old