When using std::sort, how can I overload the custom comparison function that I am using?
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
class Misc {
public:
// Comment out the next three lines to compile without problems.
static bool sortPair(const std::pair<int, int> &a, const std::pair<int, int> &b){
return a.first < b.first;
}
static bool sortPair(const std::pair<double, std::string> &a, const std::pair<double, std::string> &b){
return a.first < b.first;
}
};
int main () {
std::vector<std::pair<double, std::string> > u;
u.push_back(std::make_pair(10.0, "ten"));
u.push_back(std::make_pair(5.0, "five"));
u.push_back(std::make_pair(1.0, "one"));
std::sort(u.begin(), u.end(), Misc::sortPair);
for (unsigned int i=0; i< u.size(); i++){
std::cout << u.at(i).first << std::endl;
}
return 0;
}
I can't get this to compile as it complains about:
unresolved overloaded function type
I can see that using sortPair could be somewhat ambiguous, but I assumed that the compiler would be able to resolve this based on the types associated with the vector u. Is there some way that I could specify which function/method to use in order to disambiguate the problem?
Currently, commenting out the first sortPair function allows the code to be compiled and produces the correct sorted output. Of course, this is because it is not longer ambiguous.
&.static_cast.