fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <map>
  4. using namespace std;
  5.  
  6. map<char,int> frequencies;
  7.  
  8. void counting(char c) {
  9. // add one to the frequency-count for character c.
  10. frequencies[c]++;
  11. }
  12.  
  13. void printing(pair<char,int> the_pair){
  14. cout << the_pair.first << " " << the_pair.second << endl;
  15. }
  16.  
  17. string s="classes, containers and maps";
  18.  
  19. int main ()
  20. {
  21. // give each letter in s to the counting function, then
  22. // print the resulting frequencies
  23. for_each(s.begin(), s.end(), counting);
  24. for_each(frequencies.begin(), frequencies.end(), printing);
  25. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
  3
, 1
a 4
c 2
d 1
e 2
i 1
l 1
m 1
n 3
o 1
p 1
r 1
s 5
t 1