I have a struct which has a unique key. I want to insert instances of these structs into a set. I know that to do this the < operator has to be overloaded so that set can mak
You can overload the operator <
inside the class also as,
struct foo
{
int key;
bool operator < (const foo &other) const { return key < other.key; }
};
In your question, if you want to use set<foo> bar;
as declaration then, you should insert value as,
bar.insert(*test);
But that won't be a good idea, as you are making redundant copy.