I want to use the std::list STL container to be able to apply an order on the items contained. However, the items I want to use are a custom typedef consisting of a custom type and a double which represents a weight. How can I make sure that I can sort the resulting list based on the weight of the elements?
typedef std::pair<linearVariable*, double> weightedVariable;
[…]
std::list<weightedVariable> tmp;
What do I have to do to make sure that the list is sorted according to the second part of the pair (the weight)? Is there any better way to maintain an order of custom types? In my case the weight is not a property of the linearVariable, but it can be calculated.
You can use
std::list::sortwith a custom comparison function:Another option is to provide a
bool operator<and usestd::list::sort():