I wrote the following simple code:
std::set<Edge> edges;
Edge edge1("a","b");
Edge edge2("a","c");
edges.insert(edge1);
if (edges.find(edge2) != edges.end()) //edge2 already exists
std::cout << "OH NO!";
Which has a bug since OH NO! gets printed even though edge1 and edge2 are different from each other.
Here's how I have implemented operator == and operator < for Edge:
bool operator==(const Edge &e1, const Edge &e2) {
return (e1.source == e2.source) && (e1.destination == e2.destination);
}
bool operator<(const Edge &e1, const Edge &e2) {
return e1.source < e2.source;
}
What is causing this bug?