I was trying to implement Kruskal's algorithm (implementation of Introduction to Algorithms CLRS) using C++. But when trying to sort the Edge (which is the class i created) set (which i have implemented as a vector) using std::sort , it doesnt work. I tried using lambda function as comparator as well as overloaded '<' operator.
std::vector <Edge> givenEdgeSet;
This is the function call
std::sort(givenEdgeSet.begin(), givenEdgeSet.end());
And the overloaded operator definition is
bool operator < (const Edge& b){
//length is the edge length here..
return (length < b.length);
}
Problem appears to be in swapping of the objects in vector (which isn happening)
I have defined copy and move constructors and assignment operators Could it be a problem in the constructors ?
// copy constructor
Edge(const Edge& e){
start_point = e.start_point;
end_point = e.end_point;
length = e.length;
start_vertex_set = e.start_vertex_set;
end_vertex_set = e.end_vertex_set;
}
// copy assignment operator
Edge& operator = (const Edge& e){
std::shared_ptr<Edge> NewEdge(new Edge());
NewEdge->start_point = e.start_point;
NewEdge->end_point = e.end_point;
NewEdge->length = e.length;
NewEdge->start_vertex_set = e.start_vertex_set;
NewEdge->end_vertex_set = e.end_vertex_set;
return *NewEdge;
}
// Move Constructor
Edge(const Edge&& e){
start_point = e.start_point;
end_point = e.end_point;
length = e.length;
start_vertex_set = e.start_vertex_set;
end_vertex_set = e.end_vertex_set;
}
// Move assignment operator
Edge& operator = (const Edge&& e){
std::shared_ptr<Edge> NewEdge(new Edge());
NewEdge->start_point = e.start_point;
NewEdge->end_point = e.end_point;
NewEdge->length = e.length;
NewEdge->start_vertex_set = e.start_vertex_set;
NewEdge->end_vertex_set = e.end_vertex_set;
return (*NewEdge);
}