ぼるじょあがC/C++の宿題を片づけますYO! 20代目

このエントリーをはてなブックマークに追加
スレ汚しスマソ。やり直し。
#include <iostream>
#include <iomanip>
#include <fstream>
#include <map>
#include <string>
#include <algorithm>
#include <utility>

int main()
{
  using namespace std;
  map<string, int> m;
  string str;
  ifstream fin("data.txt");
  while (fin >> str) ++m[str];
  vector<pair<string, int> > vec(m.begin(), m.end());
  struct pred
  {
    bool operator()(const pair<string, int>& x, const pair<string, int>& y)
    {return x.second > y.second;}
  };
  stable_sort(vec.begin(), vec.end(), pred());
  struct print
  {
    void operator()(const pair<string, int>& x)
    {cout << setw(3) << x.second << ' ' << x.first << '\n';}
  };
  for_each(vec.begin(), vec.end(), print());
  cout << flush;
  return 0;
}