Standard Library
Map Lookup
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
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;
}
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);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=2return 0;
24 return 0;25}
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);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=4return 0;
24 return 0;25}
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);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=missingreturn 0;
24 return 0;25}