fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5.  
  6. struct Struct
  7. {
  8. Struct(long value) :
  9. _value_1(value),
  10. _value_2(10 - value)
  11. {}
  12.  
  13. long _value_1;
  14. long _value_2;
  15. };
  16.  
  17. template <typename StructureType,
  18. typename MemberType,
  19. MemberType StructureType::*member>
  20. bool comparator(const StructureType& the_first, const StructureType& the_second)
  21. {
  22. return the_first.*member < the_second.*member;
  23. }
  24.  
  25. int main(int argc, char const *argv[])
  26. {
  27.  
  28. std::vector<Struct> vect = { 1, 2, 3, 4, 5 };
  29. auto result_1 =
  30. std::max_element(std::begin(vect), std::end(vect),
  31. comparator<Struct, long, &Struct::_value_1>);
  32. auto result_2 =
  33. std::max_element(std::begin(vect), std::end(vect),
  34. comparator<Struct, long, &Struct::_value_2>);
  35.  
  36. std::cout
  37. << "Result 1:" << result_1->_value_1 << std::endl
  38. << "Result 2:" << result_2->_value_2 << std::endl;
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Result 1:5
Result 2:9