fork download
  1. #include <string>
  2. #include <vector>
  3. #include <map>
  4. #include <algorithm>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10. string characters = "aasa asdfs dfh f ukjyhkh k wse f sdf sdfsdf";
  11.  
  12. // initialize with 256 entries, one for each character:
  13. vector<int> counts(256);
  14.  
  15. for (string::size_type i = 0; i <= characters.length(); i++) {
  16. // for each occurrence of a character, increase the value in the vector:
  17. int int_char = (int)characters[i];
  18. counts[int_char]++;
  19. }
  20. vector<int>::iterator most_frequent =
  21. std::max_element(counts.begin(), counts.end());
  22.  
  23. // getting the character (index within the container, "key"):
  24. cout << (char)(most_frequent - counts.begin()) << ": ";
  25.  
  26. // the number of occurrences ("value"):
  27. cout << (*most_frequent) << endl;
  28.  
  29.  
  30. // Sorted statistics:
  31. multimap<int,int> keyForOccurrence;
  32. for (vector<int>::iterator i = counts.begin(); i != counts.end(); ++i) {
  33. int occurrences = *i;
  34. int character = i - counts.begin();
  35. keyForOccurrence.insert(pair<int,int>(occurrences, character));
  36. }
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
 : 9