C/C++ Arena

Step 2 of 6

Look up without inserting

m[key] inserts missing keys, which is not what you want for a pure lookup. Use find or contains (C++20):

auto it = prices.find("awp");
if (it != prices.end()) {
    int p = it->second;       // it->first is the key
}
if (prices.contains("awp")) { ... }

Your turn: write int price_of(const std::map<std::string, int>& prices, const std::string& item) that returns the price, or -1 if the item isn't in the map. (The map is const, so [] isn't even allowed.)

Previous: std::map Next: std::set