If you can use boost , then you can write a lambda expression for the binary predicate expected by max_element :
struct A { A(int n): x(n) { } int x; }; using namespace std; using namespace boost::lambda; int main() { vector<A> as; as.push_back(A(7)); as.push_back(A(5)); as.push_back(A(3)); vector<A>::iterator iter = max_element(as.begin(), as.end(), bind(&A::x, _2) > bind(&A::x, _1)); int max = iter->x; }
Naveen
source share