1// sort word by frequency, in-memory version.
2#include <algorithm>
3#include <iostream>
4#include <unordered_map>
5#include <vector>
6
7typedef std::unordered_map<std::string, int> WordCount;
8
9int main()
10{
11  WordCount counts;
12  std::string word;
13  while (std::cin >> word)
14  {
15    counts[word]++;
16  }
17
18  std::vector<std::pair<int, WordCount::const_iterator>> freq;
19  freq.reserve(counts.size());
20  for (auto it = counts.cbegin(); it != counts.cend(); ++it)
21  {
22    freq.push_back(make_pair(it->second, it));
23  }
24
25  std::sort(freq.begin(), freq.end(), [](const std::pair<int, WordCount::const_iterator>& lhs,  // const auto& lhs in C++14
26                                         const std::pair<int, WordCount::const_iterator>& rhs) {
27    return lhs.first > rhs.first;
28  });
29  // printf("%zd\n", sizeof(freq[0]));
30  for (auto item : freq)
31  {
32    std::cout << item.first << '\t' << item.second->first << '\n';
33  }
34}
35