I want to overload the left-hand operator (<) for class Node. Note that, the elements won't be the class objects it will be a pointer to them. Please see the set<Node*>
defined in the main class.
The one I have written right now doesn't work. I have also tried the friend function, declaring overloading outside the class as a non member function, but it doesn't work either. Doesn't work mean elements are randomly ordered in the set, and the comparator is not called. Whereas, it should be ordered as per the defination of my comparator.
#include <iostream>
#include <set>
using namespace std;
class Node {
public:
int x, y;
Node *prev, *next;
Node(int x, int y) {
this->x = x; this->y = y;
this->prev = this->next = nullptr;
}
bool operator<(const Node& node) const {
return this->x < node.x;
}
};
int main() {
set<Node*> S;
S.insert(new Node(2, 4));
S.insert(new Node(3, 2));
S.insert(new Node(1, 4));
S.insert(new Node(5, 1));
S.insert(new Node(4, 3));
for (auto itr : S)
cout << itr-> x << endl;
return 0;
}