fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <map>
  4. #include <string>
  5. #include <utility>
  6. using namespace std;
  7.  
  8. int main() {
  9. const string data = "foofaaster";
  10.  
  11. // populate the map
  12. map <char, int> chars;
  13. for (auto c = data.begin(); c != data.end(); ++c)
  14. chars [*c]++;
  15.  
  16. // Now display the occurances
  17. cout << "Original data: '" << data << "'" << endl;
  18. multimap <int, char, greater <int>> counts;
  19. for (auto c = chars.begin(); c != chars.end(); ++c)
  20. counts.insert (make_pair (c->second, c->first));
  21. cout << "Character counts:" << endl;
  22. for (auto it = counts.begin(); it != counts.end(); ++it)
  23. cout << "\t" << it->first << ": '" << it->second << "'" << endl;
  24.  
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Original data: 'foofaaster'
Character counts:
	2: 'a'
	2: 'f'
	2: 'o'
	1: 'e'
	1: 'r'
	1: 's'
	1: 't'