For this piece of code below:
int main()
{
std::set<Node> s;
for (int i = 0; i <= 5; i++)
s.insert(Node(i));
s.insert(Node(4));
for (auto itor = s.begin(); itor != s.end(); itor++)
{
std::cout << itor->val << ' ';
}
}
When the sign '<' is overwrote as below, the output is: '5 4 3 2 1 0'
struct Node
{
int val;
Node(int _val = -1) : val(_val) {}
bool operator<(const Node &p) const
{
return val > p.val;
}
};
When I change the function into this:
bool operator<(const Node &p) const
{
return val >= p.val;
}
The output changes into: '5 4 4 3 2 1 0'. The difference confuses me, could someone explain why this happened and explain the principles of the 'insert' function?