我想用这个对一个向量v进行排序
std::sort(v.begin(),v.end(),cmpr);我的cmpr函数在哪里
bool cmpr(int a,int b, int c)
{
return a%c <= b%c;
}现在我想知道怎样才能通过c?
发布于 2020-03-30 13:23:59
您可以使用lambda包装您的比较器。完整的示例:
#include <algorithm>
#include <iostream>
auto make_cmpr(int c)
{
return [c](int a, int b) {
return a%c <= b%c;
};
}
int main()
{
int a[5] = {2, 4, 1, 3, 5};
std::sort(a, a + 5, make_cmpr(3));
/* or directly
int c = 3;
std::sort(a, a + 5,
[c](int a, int b) {
return a%c <= b%c;
}
);
*/
for (int e : a) std::cout << e << ' ';
}https://stackoverflow.com/questions/60924115
复制相似问题