fork(5) download
  1. #include <iostream>
  2. #include <map>
  3.  
  4. struct record
  5. {
  6. const char* id;
  7. int count;
  8.  
  9. ~record() { std::cout << "~record: " << count << std::endl; }
  10. };
  11.  
  12. const char* foo()
  13. {
  14. std::map< const char*, size_t > counters;
  15. for (const record& r : std::initializer_list<record>{
  16. { "a", 1 },
  17. { "a", 2 },
  18. { "b", 3 },
  19. { "b", 4 },
  20. { "a", 5 },
  21. { "a", 6 },
  22. { "c", 7 },
  23. { "c", 8 },
  24. { "a", 9 },
  25. })
  26. {
  27. std::cout << r.count << std::endl;
  28. counters[r.id] += r.count;
  29. }
  30. return counters.begin()->first;
  31. };
  32.  
  33. int main()
  34. {
  35. std::cout << foo()[0] << std::endl;
  36. }
  37.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1
2
3
4
5
6
7
8
9
~record: 9
~record: 8
~record: 7
~record: 6
~record: 5
~record: 4
~record: 3
~record: 2
~record: 1
a