I am writing a code to solve the following problem: Given a set of numbers x[0], x[1], ..., x[N-1], find the permutation that makes them sorted in the ascending order. In the other words, I would like to find a permutation on {0,2,...,N-1} such as i[0], i[1], ..., i[N-1] such that x[i[0]] <= x[i[1]] <= ... <= x[i[N-1]].
For this, I have stored the x vector and an index vector i (initially filled with i[j] = j) as private members of a class. I have also defined a private method as
bool MyClass::compare(size_t s, size_t t) {
return (x[s] < x[t]);
}
Now, I would call the std::sort as follows
std::sort(i.begin(), i.end(), compare);
and I expect to get the desired result. But the code does not compile and I get the following error:
error: no matching function for call to ‘sort(std::vector<long unsigned int>::iterator, std::vector<long unsigned int>::iterator, <unresolved overloaded function type>)’
I must have done everything correctly as also the documentation of std::sort mentions that I can pass a function as the compare operator to std::sort (http://www.cplusplus.com/reference/algorithm/sort/)
Thanks for all the helps in advance.