(This is a related question, but there are difference with my case that makes me doubt my understanding of it).
I have this class:
class MyOwnClass
{
public:
int score; Specialcustomtype val1; double index;
private:
};
and a vector of MyOwnClass
vector<MyOwnClass> MySuperVector(20);
Having some code that set values to the fields of MyOwnClass, I want to find which MyOwnClass in the vector has the field score with the highest value.
In an answer from the related questions :
#include <algorithm> // For std::minmax_element
#include <tuple> // For std::tie
#include <vector> // For std::vector
#include <iterator> // For global begin() and end()
struct Size {
int width, height;
};
std::vector<Size> sizes = { {4, 1}, {2, 3}, {1, 2} };
decltype(sizes)::iterator minEl, maxEl;
std::tie(minEl, maxEl) = std::minmax_element(begin(sizes), end(sizes),
[] (Size const& s1, Size const& s2)
{
return s1.width < s2.width;
});
But in my case, the fields of MyOwnClass are of different types and my attempts at using "max_elements" have failed.
Of course I could loop among the n elements of the vector and use a comparison to find which object has the highest score, and it works but I am sure that the built-in functions of c++ are more efficient thant my version.