我想使用 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);
无效使用非静态成员函数compareAscending
使排序函数成为非类成员或使其成为static
- 或使用 lambda:
std::sort(vect.begin(), vect.end(),
[](const std::vector<int>& v1, const std::vector<int>& v2) {
return v1[0] < v2[0];
}
);
A 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)