1.Word frequency with map
IntermediateCount the words in "red blue red green blue red" and print each word with its count, in alphabetical order.
Show hint
++counts[word] creates an entry with 0 the first time, then increments it. A std::map keeps its keys sorted.
Show solution
#include <iostream>
#include <map>
#include <sstream>
#include <string>
int main() {
std::istringstream in("red blue red green blue red");
std::map<std::string, int> counts;
std::string word;
while (in >> word) {
++counts[word];
}
for (const auto& [w, n] : counts) {
std::cout << w << ' ' << n << '\n';
}
}blue 2
green 1
red 3