I want to declare set of std::unique_ptr<A>
like this:
class A
{
public:
///type of itself
typedef A self;
///Simple constructor
A(int w) : weight(w) {}
///To use for sorting
bool operator<(const self& another)
{
return weight < another.weight;
}
private:
int weight;
};//class A
//Container of pointers to A sorted by object, not by pointer values.
std::set(std::unique_ptr<A>, /*something*/) storage_for_A;
So, what to use in something? The obvious way is to declare a class for comparing two std::unique_ptr<A>
by value of A
and use it in above container declaration. However is there a better way? May be something predefined exists already? Surely, the problem is pretty standard.
Update: I know there is a question "Order of elements in set of pointers", but this is not what I am asking here. I know how set of pointers works. And I know about the solution, suggested there, which is mentioned in my question. I explicitly asked whether solution without custom-defined class for comparison is possible. E.g. through use of lambda functions?