如何将参数传递到自定义比较C 中分类的函数
How to pass parameters to custom compare function for sorting in C++
我有一个自定义比较函数:
bool compare(int a, int b){
if(a>2*b) return true;
return false;
}
现在我想这样使用此功能:
vector<int> numbers;//say it contains random numbers
sort(numbers.begin(), numbers.end(), compare(a, b));//a and b are the numbers that sort function currently compares to each other
显然,此代码不起作用,因为该程序不知道A和B是什么。我的问题是,如何将所需的数字传递到比较函数?
这里有一个很好的答案,关于如何使用std :: sort
https://stackoverflow.com/a/1380496/6115571
在另一个注意事项上,您实际上不需要两个返回语句
bool compare(int a, int b) {
return (a>2*b);
}
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 如何将参数传递到自定义比较C 中分类的函数
- 我一直在尝试递归中编写一个分类的链接列表函数
- 使用lambda函数将向量与另一个向量进行分类
- 为什么在对自定义对象的向量进行分类时,我们将在struct的定义中包括比较函数
- 函数指针类型和值的部分类专用化
- 如何通过复制构造函数区分类中的每个变量
- 如何区分类成员函数的声明和数据成员的声明
- 使用 OpenCV 级联分类器时在函数_main中引用的未解析外部符号