map is sorted by key. unordered_map uses hashing (faster, but unsorted):
#include <map>
map<string, int> ages;
ages["Alice"] = 30;
ages["Bob"] = 25;
cout << ages["Alice"]; // 30
cout << ages.count("Dave"); // 0 (missing keys return 0)
ages.erase("Bob");
for (auto& [name, age] : ages) { // structured bindings (C++17)
cout << name << ": " << age;
}
if (ages.find("Alice") != ages.end()) {
cout << "Found!";
}
Write wordCount(string text) returning a map<string,int> with the frequency of each word.
Example: "the cat sat on the mat" → {cat:1, mat:1, on:1, sat:1, the:2}
Click "Run" to execute your code.