#include <iostream>
#include <vector>
#include <algorithm>
struct Struct
{
Struct(long value) :
_value_1(value),
_value_2(10 - value)
{}
long _value_1;
long _value_2;
};
template <typename StructureType,
typename MemberType,
MemberType StructureType::*member>
bool comparator(const StructureType& the_first, const StructureType& the_second)
{
return the_first.*member < the_second.*member;
}
int main(int argc, char const *argv[])
{
std::vector<Struct> vect = { 1, 2, 3, 4, 5 };
auto result_1 =
std::max_element(std::begin(vect), std::end(vect),
comparator<Struct, long, &Struct::_value_1>);
auto result_2 =
std::max_element(std::begin(vect), std::end(vect),
comparator<Struct, long, &Struct::_value_2>);
std::cout
<< "Result 1:" << result_1->_value_1 << std::endl
<< "Result 2:" << result_2->_value_2 << std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKCnN0cnVjdCBTdHJ1Y3QKewoJU3RydWN0KGxvbmcgdmFsdWUpIDoKCQlfdmFsdWVfMSh2YWx1ZSksCgkJX3ZhbHVlXzIoMTAgLSB2YWx1ZSkKCXt9CgoJbG9uZyBfdmFsdWVfMTsKCWxvbmcgX3ZhbHVlXzI7Cn07Cgp0ZW1wbGF0ZSA8dHlwZW5hbWUgU3RydWN0dXJlVHlwZSwgCgkJCXR5cGVuYW1lIE1lbWJlclR5cGUsIAoJCQlNZW1iZXJUeXBlIFN0cnVjdHVyZVR5cGU6OiptZW1iZXI+CmJvb2wgY29tcGFyYXRvcihjb25zdCBTdHJ1Y3R1cmVUeXBlJiB0aGVfZmlyc3QsIGNvbnN0IFN0cnVjdHVyZVR5cGUmIHRoZV9zZWNvbmQpCnsKCXJldHVybiB0aGVfZmlyc3QuKm1lbWJlciA8IHRoZV9zZWNvbmQuKm1lbWJlcjsKfQoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIgY29uc3QgKmFyZ3ZbXSkKewoJCglzdGQ6OnZlY3RvcjxTdHJ1Y3Q+IHZlY3QgPSB7IDEsIDI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