1df3173cbSShuo Chen// sort word by frequency, in-memory version.
2144e8e4eSShuo Chen#include <algorithm>
3144e8e4eSShuo Chen#include <iostream>
4144e8e4eSShuo Chen#include <unordered_map>
5144e8e4eSShuo Chen#include <vector>
6144e8e4eSShuo Chen
7144e8e4eSShuo Chentypedef std::unordered_map<std::string, int> WordCount;
8144e8e4eSShuo Chen
9144e8e4eSShuo Chenint main()
10144e8e4eSShuo Chen{
11144e8e4eSShuo Chen  WordCount counts;
12144e8e4eSShuo Chen  std::string word;
13144e8e4eSShuo Chen  while (std::cin >> word)
14144e8e4eSShuo Chen  {
15144e8e4eSShuo Chen    counts[word]++;
16144e8e4eSShuo Chen  }
17144e8e4eSShuo Chen
188107c90fSShuo Chen  std::vector<std::pair<int, WordCount::const_iterator>> freq;
19144e8e4eSShuo Chen  freq.reserve(counts.size());
208107c90fSShuo Chen  for (auto it = counts.cbegin(); it != counts.cend(); ++it)
21144e8e4eSShuo Chen  {
22144e8e4eSShuo Chen    freq.push_back(make_pair(it->second, it));
23144e8e4eSShuo Chen  }
24144e8e4eSShuo Chen
25f4752293SShuo Chen  std::sort(freq.begin(), freq.end(), [](const std::pair<int, WordCount::const_iterator>& lhs,  // const auto& lhs in C++14
26f4752293SShuo Chen                                         const std::pair<int, WordCount::const_iterator>& rhs) {
27144e8e4eSShuo Chen    return lhs.first > rhs.first;
28144e8e4eSShuo Chen  });
29144e8e4eSShuo Chen  // printf("%zd\n", sizeof(freq[0]));
30144e8e4eSShuo Chen  for (auto item : freq)
31144e8e4eSShuo Chen  {
32144e8e4eSShuo Chen    std::cout << item.first << '\t' << item.second->first << '\n';
33144e8e4eSShuo Chen  }
34144e8e4eSShuo Chen}
35