FINDING MAX VAL IN A OBJECT

Hi

A vector contains a few object, in those objects a certain member type is age which is a number. How to find the max age from the vector of objects using max_element algorithm.

How to write a lambda expression to get max date
Something like this.

1
2
3
4
 auto iterator_to_max_age_element = std::max_element(theVector.begin(), theVector.end(),
            [] (Object const lhs, Object const  rhs) {
            return lhs.age < rhs.age;  });
cout << "Max age is " << (*iterator_to_max_age_element).age;



Chane it for your variable names, and the names of member variables etc
Last edited on
You know about the algorithm and thus you can read:
http://www.cplusplus.com/reference/algorithm/max_element/

https://en.cppreference.com/w/cpp/algorithm/sort
has an example that has lambda in it.

https://www.walletfox.com/course/sortvectorofcustomobjects11.php has more about lambda.
Topic archived. No new replies allowed.