[ create a new paste ] login | about

Link: http://codepad.org/eLeiHNaG    [ raw code | output | fork ]

C++, pasted on Jun 22:
#include <algorithm>
#include <iterator>
#include <iostream>
#include <map>
#include <utility>
#include <vector>

using namespace std;

struct pair2ndGreater
{
  template<class T, class U>
  bool operator()(pair<T,U> const& a, pair<T,U> const& b) const
  { return a.second > b.second; }
};

int main()
{
  map<char,int> histogramm;
  const char* s = "Dies ist ein Text";
  while (*s) ++histogramm[*s++];
  vector<pair<char,int> > vec (histogramm.begin(),histogramm.end());
  sort(vec.begin(),vec.end(),pair2ndGreater());
  for (int i=0, e=vec.size(); i<e; ++i) {
    pair<char,int> const& p = vec.at(i);
    cout << '"' << p.first << "\" --> " << p.second << '\n';
  }
}


Output:
1
2
3
4
5
6
7
8
9
" " --> 3
"e" --> 3
"i" --> 3
"s" --> 2
"t" --> 2
"D" --> 1
"T" --> 1
"n" --> 1
"x" --> 1


Create a new paste based on this one


Comments: