fork(3) download
  1. #include <cctype>
  2. #include <iostream>
  3. #include <map>
  4. #include <sstream>
  5. #include <string>
  6. #include <vector>
  7.  
  8. int main(int argc, char** argv)
  9. {
  10. std::vector<int> nums;
  11. nums.push_back(5789);
  12. nums.push_back(-9876);
  13. nums.push_back(12345);
  14. nums.push_back(56789);
  15. nums.push_back(56788);
  16. nums.push_back(-1111);
  17.  
  18. std::map<char, int> digit_frequency;
  19.  
  20. for(int i = 0; i < nums.size(); ++i)
  21. {
  22. std::stringstream ss;
  23. ss << nums[i];
  24. std::string str = ss.str();
  25. if(isdigit(str[0]))
  26. {
  27. ++digit_frequency[str[0]];
  28. }
  29. else if(isdigit(str[1]))
  30. {
  31. ++digit_frequency[str[1]];
  32. }
  33. }
  34.  
  35. std::map<char, int>::iterator it;
  36. for(it = digit_frequency.begin(); it != digit_frequency.end(); ++it)
  37. {
  38. std::cout << "Number " << it->first << " occurred " << it->second << " time(s).\n";
  39. }
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 3436KB
stdin
Standard input is empty
stdout
Number 1 occurred 2 time(s).
Number 5 occurred 3 time(s).
Number 9 occurred 1 time(s).