使用 vector.sort() 的非静态成员函数无效使用

Invalid use of non-static member function using vector.sort()

本文关键字:函数 静态成员 无效 vector sort 使用      更新时间:2023-10-16

我想使用 sort(( 函数根据其第一列对二维向量进行排序,但不幸的是,通过传递"compareAscending"函数,我收到"无效使用非静态成员函数 compareAscending"错误。

我也尝试使函数静态,但遇到了同样的问题。

static bool compareAscending(const std::vector<int>& v1, const std::vector<int>& v2) 
{ 
return (v1[0] < v2[0]); 
} 

这是我想用于排序功能的比较器

bool compareAscending(const std::vector<int>& v1, const std::vector<int>& v2) 
{ 
return (v1[0] < v2[0]); 
} 

这就是我想调用的排序函数

sort(vect.begin(), vect.end(), compareAscending);

无效使用非静态成员函数 compare升序

使排序函数成为非类成员或使其static- 或使用 lambda:

std::sort(vect.begin(), vect.end(),
[](const std::vector<int>& v1, const std::vector<int>& v2) {
return v1[0] < v2[0];
}
);

static版本:

class foo {
public:
static bool compareAscending(const std::vector<int>& v1,
const std::vector<int>& v2) {
return v1[0] < v2[0];
}
};
std::sort(vect.begin(), vect.end(), foo::compareAscending);

免费函数版本:

bool compareAscending(const std::vector<int>& v1,
const std::vector<int>& v2) {
return v1[0] < v2[0];
}
std::sort(vect.begin(), vect.end(), compareAscending);