[Solved] Why sort() function causes compilation error when it is used for set of class objects


Two problems. First, you cannot reorder the elements of a set. Their ordering criteria is determined upon construction, it is a fundamental part of the object. This is necessary in order for it to achieve O(log n) lookups, insertions, and deletions, which is part of the promises of std::set. By default, it will use std::less<Edge>, which should call your operator<. But you could also use your edge_comparator function, like this:

std::set<Edge, bool(*)(Edge&,Edge&)> edge_set(edge_comparator);

Second, std::sort can only be used on random access iterators or better, and std::set iterators are bi-directional.

2

solved Why sort() function causes compilation error when it is used for set of class objects