我正在编写代码来解决以下问题:给定一组数字x[0]
, x[1]
, ..., x[N-1]
,找到使它们按升序排序的排列。换句话说,我想找到 {0,2,...,N-1} 上的排列,例如i[0]
, i[1]
, ..., i[N-1]
这样x[i[0]] <= x[i[1]] <= ... <= x[i[N-1]]
.
为此,我存储了x
向量和索引向量i
(最初充满i[j] = j
)作为类的私有成员。我还定义了一个私有方法
bool MyClass::compare(size_t s, size_t t) {
return (x[s] < x[t]);
}
现在,我会打电话给std::sort
如下
std::sort(i.begin(), i.end(), compare);
我希望得到想要的结果。但代码无法编译,并且出现以下错误:
error: no matching function for call to ‘sort(std::vector<long unsigned int>::iterator, std::vector<long unsigned int>::iterator, <unresolved overloaded function type>)’
我必须正确地完成所有事情以及文档std::sort
提到我可以将函数作为比较运算符传递给std::sort
(http://www.cplusplus.com/reference/algorithm/sort/ http://www.cplusplus.com/reference/algorithm/sort/)
提前感谢您的所有帮助。