I have this simple program to count the word frequency:
#include <iostream>
#include <map>
#include <cctype>
#include <string>
using namespace std;
int main(void) {
map<string, int> words;
map<string, int>::iterator i;
string s;
while (cin >> s) {
transform(s.begin(), s.end(), s.begin(), ::tolower);
words[s]++;
}
for (i=words.begin(); i != words.end(); i++)
cout << i->first << " " << i->second << endl;
return 0;
Now how do I make it to count line frequency instead of word frequency. Each line is separated by ‘\n’ new line character in the input.
If you want to count the total lines, then its very simple:
But if you really want to count frequency of same line, then in your code, replace
with
where
lineis defined as:Or, you can make it generic:
then define these typedefs: