I'm trying to write a program for every word in stdin, output a list of pairs of the form L:N
where L
is a line number and N
is the number of occurrences of the given word.
So if stdin is:
hello world
hello hello
the output should be
hello 1:1 2:2
world 1:1
In the code below
#include <iostream>
#include <map>
#include <string>
#include <iterator>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::map;
using std::pair;
int main(int argc, const char *argv[])
{
map<string, pair<unsigned int, unsigned int>> table;
string word;
while (cin >> word) {
++table[word];
}
for (std::map<string, pair<unsigned int, unsigned int>>::iterator itr = table.begin(); itr != table.end();
++itr) {
cout << itr->first << "\t => \t" << itr->second << itr->third << endl;
}
while (cin >> word) {
++table[word];
}
}
I'm trying to make a map that uses three elements and have an iterator that can traverse through the map as well as count the number of lines and use getline()
to get the number of occurrences of a word on each line. This code just outputs just the total word count.