-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathumap_count_freq.cpp
More file actions
33 lines (25 loc) · 892 Bytes
/
Copy pathumap_count_freq.cpp
File metadata and controls
33 lines (25 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
find freq of every word in a string, using unordered_map
*/
#include <iostream>
#include <unordered_map>
#include <sstream>
using std::cout;
using std::endl;
int main()
{
std::string instr = "here we go round the mulberry bush "
"the mulberry bush "
"the mulberry bush";
std::unordered_map<std::string, int> wordFreqs;
// break the input string into word using stringstream
std::stringstream ss(instr); // used for breaking words
std::string word; // to store individual words
while (ss >> word)
wordFreqs[word]++;
// iterate over (word,freq) pair and print
std::unordered_map<std::string, int>:: iterator itr;
for (itr = wordFreqs.begin(); itr != wordFreqs.end(); itr++)
cout << "(" << itr->first << ", " << itr->second << ")" << endl;
return 0;
}