我正在编写一段代码来解决以下问题:给定一组数字
为此,我已将
现在,我会像这样调用
我一定做对了,因为
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/)。谢谢您的帮助。