fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4.  
  5. typedef std::vector<std::vector<double> > matrix_t;
  6. struct stats {
  7. int count = 0;
  8. double total = 0.0;
  9. };
  10. typedef std::map<double, stats> results_t;
  11.  
  12. results_t aggregate(matrix_t const& matrix)
  13. {
  14. results_t results;
  15. for (auto const& row: matrix)
  16. {
  17. auto const& id = row[0];
  18. auto const& value = row[1];
  19. auto& stats = results[id];
  20. ++stats.count;
  21. stats.total += value;
  22. }
  23.  
  24. return results;
  25. }
  26.  
  27. int main()
  28. {
  29. matrix_t matrix = {
  30. {1, 10},
  31. {2, 20},
  32. {1, 30},
  33. {2, 40},
  34. {3, 60}
  35. };
  36.  
  37. std::cout << "ID" << "\tCOUNT" << "\tMEAN" << std::endl;
  38. for (auto const& row: aggregate(matrix))
  39. {
  40. auto stats = row.second;
  41. std::cout << row.first << '\t' << stats.count << '\t' << stats.total / stats.count << std::endl;
  42. }
  43. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
ID	COUNT	MEAN
1	2	20
2	2	30
3	1	60