A vector works well when you need a sequence. A map is useful when you want to associate a unique key with a value, such as a product name with a stock count. std::map<std::string, int> from <map> stores string keys paired with integer values. Its usual iteration order follows sorted keys, not insertion order.
The initializer {{"pens", 8}, {"cards", 5}} is a list of entries. Each inner pair supplies one key and its associated value. A map has at most one entry for each key. Assigning with stock["pens"] = 9 updates that key, but using brackets with a missing key inserts a new entry with a default value first. Brackets are therefore not a read-only missing-key check.
Use find(key) to look up without insertion. It returns an iterator or end() when absent. At a valid iterator, position->first is the key and position->second is the mapped value. The arrow accesses a member through the iterator, much like it does through a pointer.
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> stock{{"pens", 8}, {"cards", 5}};
auto position = stock.find("cards");
if (position != stock.end()) { std::cout << position->second << '\n'; }
}
The example prints 5. Your helper already calls find and returns zero when the key is absent. By the time execution reaches the temporary return -1, the iterator refers to a real entry, so it is safe to read its mapped value. Keep that guard in place. Zero is the chosen missing-key result here, but it could also be a stored quantity; callers cannot use zero alone to decide whether a key exists.
position->second. The probes should report Cups: 12, Napkins: 0, and the unchanged Keys: 2.Edit main.cpp, then press Run to inspect the Console. Press Submit to check your work. Keep the supplied probes in place.