I have a vector of pointers to objects QActionDonald and I’m trying to find the object containing the highest expectedvalue_. I have overloaded the operator< and I’m using the max_element method to obtain an iterator to the highest value but i always receive the end value which is not correct.
QActionDonald.h:
class QActionDonald
{
public:
double expectedvalue_;
bool operator < (const QActionDonald& other) const;
};
QActionDonald.cpp:
bool QActionDonald::operator< (const QActionDonald& other) const
{
return expectedvalue_ < other.expectedvalue_;
}
otherClass:
std::vector<QActionDonald*> *actionList = qValueDictionary[stateKey];
std::vector<QActionDonald*>::iterator it =
std::max_element(actionList->begin(), actionList->end());
Of course it’s not, because your container has pointers, not objects.
For it to be used, you’ll need
std::vector<QActionDonald>or, if that’s not subject to change, to provide a custom comparator tostd::max_elementthat applies the correct logic (i.e. compare objects, not pointers).