Practice: Word Frequency
Use a map to count words.
Example
#include <iostream>
#include <map>
#include <sstream>
#include <string>
int main() {
std::istringstream in("the cat and the hat and the bat");
std::map<std::string, int> freq;
std::string w;
while (in >> w) freq[w]++;
for (auto &[k, v] : freq) std::cout << k << ":" << v << " ";
std::cout << "\n";
return 0;
}Result
and:2 bat:1 cat:1 hat:1 the:3
Exercise
Increment.
freq[w]Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.