C/C++ Arena

Step 1 of 6

std::map

std::map<Key, Value> stores key/value pairs sorted by key. m[key] gets the value, creating it (as 0 or empty) if it doesn't exist yet, which makes counting easy:

std::map<std::string, int> kills;
kills["ropz"] += 3;
kills["rain"]++;
for (const auto& [name, k] : kills) {     // sorted by name
    std::cout << name << " " << k << "\n";
}

The [name, k] syntax is a structured binding that unpacks each pair.

Your turn: count how many times each word appears in the input and print word count lines in alphabetical order.

Next: Look up without inserting