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

map lookup A map lookup checks whether a key exists before reading the value for that key.

Map Lookup

item
map_lookup.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <map>
#include <string>

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

    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 = "apple";

    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 = "lamp";

    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;
}
  1. item ← book, inventory ← (empty), found ← (empty)

    5int main() {6    std::string item→ book = "book"; //@item="apple", "lamp"78    std::map<std::string, int> inventory→ (empty){9        {"apple", 4},10        {"book", 2},11        {"cup", 7}12    };1314    auto found→ (empty) = inventory(empty).find(itembook);
  2. if (found != inventory.end())

    16if (found(empty) != inventory(empty).end()) {17    std::cout << "item=" << found(empty)->first << std::endl;18    std::cout << "count=" << found(empty)->second << std::endl;19} else {
    outputitem=book
    count=2
  3. return 0;

    24    return 0;25}
  1. item ← apple, inventory ← (empty), found ← (empty)

    5int main() {6    std::string item→ apple = "apple";78    std::map<std::string, int> inventory→ (empty){9        {"apple", 4},10        {"book", 2},11        {"cup", 7}12    };1314    auto found→ (empty) = inventory(empty).find(itemapple);
  2. if (found != inventory.end())

    16if (found(empty) != inventory(empty).end()) {17    std::cout << "item=" << found(empty)->first << std::endl;18    std::cout << "count=" << found(empty)->second << std::endl;19} else {
    outputitem=apple
    count=4
  3. return 0;

    24    return 0;25}
  1. item ← lamp, inventory ← (empty), found ← (empty)

    5int main() {6    std::string item→ lamp = "lamp";78    std::map<std::string, int> inventory→ (empty){9        {"apple", 4},10        {"book", 2},11        {"cup", 7}12    };1314    auto found→ (empty) = inventory(empty).find(itemlamp);
  2. else

    18    std::cout << "count=" << found->second << std::endl;19} else {20    std::cout << "item=" << itemlamp << std::endl;21    std::cout << "count=missing" << std::endl;22}
    outputitem=lamp
    count=missing
  3. return 0;

    24    return 0;25}