std::map stores key-value pairs and keeps lookup code explicit.

Map Lookup

map_lookup.cpp
#include <iostream>
#include <map>
#include <string>

int main() {
    std::string item = ;

    std::map<std::string, int> inventory{
        {"apple", 4},
        {"book", 2},
        {"cup", 7}
    };

    auto found = inventory.find(item);

    if (found != inventory.end()) {
        std::cout << "item=" << found->first << std::endl;
        std::cout << "count=" << found->second << std::endl;
    } else {
        std::cout << "item=" << item << std::endl;
        std::cout << "count=missing" << std::endl;
    }

    return 0;
}
#include <iostream>
#include <map>
#include <string>

int main() {
    std::string item = ;

    std::map<std::string, int> inventory{
        {"apple", 4},
        {"book", 2},
        {"cup", 7}
    };

    auto found = inventory.find(item);

    if (found != inventory.end()) {
        std::cout << "item=" << found->first << std::endl;
        std::cout << "count=" << found->second << std::endl;
    } else {
        std::cout << "item=" << item << std::endl;
        std::cout << "count=missing" << std::endl;
    }

    return 0;
}
#include <iostream>
#include <map>
#include <string>

int main() {
    std::string item = ;

    std::map<std::string, int> inventory{
        {"apple", 4},
        {"book", 2},
        {"cup", 7}
    };

    auto found = inventory.find(item);

    if (found != inventory.end()) {
        std::cout << "item=" << found->first << std::endl;
        std::cout << "count=" << found->second << std::endl;
    } else {
        std::cout << "item=" << item << std::endl;
        std::cout << "count=missing" << std::endl;
    }

    return 0;
}
map lookup A map lookup checks whether a key exists before reading the value for that key.