Read first
Task 55
Count with a map
`std::map` keeps keys sorted and adds a missing key automatically when you use `[]`, which makes counting simple.
Task
Task: Count with a map. What does this program print?
The editor already contains a starting point. Change it until every check passes — the Next task button unlocks when it does.
What you start with
#include <iostream>
#include <map>
using namespace std;
int main() {
string words[] = {"python", "sql", "python", "html", "python"};
map<string, int> counts;
for (const string& w : words) {
counts[w]++;
}
for (const auto& pair : counts) {
cout << pair.first << " = " << pair.second << endl;
}
return 0;
}
C++
1 automatic check Capstone: structs, maps and errors
Code editor
Ctrl + Enter runs
Run your code to see the output and the checks.
Your result
Submit your code to see results
Write your code in the middle column and press Run code.
Next task (pass every check first)
Hint
Ask yourself: Task: Count with a map. What does this program print?