fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <limits>
  5. #include <algorithm>
  6. #include <iterator>
  7. using namespace std;
  8.  
  9. struct Object {
  10. std::string name;
  11. int a;
  12. int b;
  13. int c;
  14. };
  15.  
  16. class ObjectCollection {
  17. public:
  18. std::vector<Object> collection;
  19. size_t getMaxIndexOfA();
  20. size_t getMaxIndexOfB();
  21. size_t getMaxIndexOfC();
  22. };
  23.  
  24. size_t ObjectCollection::getMaxIndexOfA() {
  25. return std::distance(collection.begin(), std::max_element(collection.begin(), collection.end(),
  26. [](auto &o1, auto &o2){ return o1.a<o2.a; }));
  27. }
  28. size_t ObjectCollection::getMaxIndexOfB() {
  29. return std::distance(collection.begin(), std::max_element(collection.begin(), collection.end(),
  30. [](auto &o1, auto &o2){ return o1.b<o2.b; }));
  31. }
  32. size_t ObjectCollection::getMaxIndexOfC() {
  33. return std::distance(collection.begin(), std::max_element(collection.begin(), collection.end(),
  34. [](auto &o1, auto &o2){ return o1.c<o2.c; }));
  35. }
  36. int main() {
  37. ObjectCollection c;
  38. c.collection.push_back({"test0" , 1,2,3 });
  39. c.collection.push_back({"test1" , 2,1,4 });
  40. c.collection.push_back({"test2" , 3,1,2 });
  41. cout << c.getMaxIndexOfA() <<" " << c.getMaxIndexOfB() <<" " << c.getMaxIndexOfC() <<endl;
  42. return 0;
  43. }
Success #stdin #stdout 0s 4364KB
stdin
Standard input is empty
stdout
2 0 1