Using std::map

This example demonstrates how to use a map to store key-value pairs.

cppCopy code#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<std::string, int> ageMap;
    
    // Inserting key-value pairs
    ageMap["Alice"] = 30;
    ageMap["Bob"] = 25;
    ageMap["Charlie"] = 35;

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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *