<分区>
我不确定如何将比较函数传递给 lower_bound 函数。当我尝试传递 4 个参数时,出现错误。我将非常感谢如何在 lower_bound 函数中传递比较函数的示例。
<分区>
我不确定如何将比较函数传递给 lower_bound 函数。当我尝试传递 4 个参数时,出现错误。我将非常感谢如何在 lower_bound 函数中传递比较函数的示例。
最佳答案
在 lower_bound
的过载中你正在使用,Compare
必须满足BinaryPredicate
的要求.
可以找到您要的示例online .
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool ignore_case(char a, char b) {
return(tolower(a) == tolower(b));
}
int main(void) {
vector<char> v = {'A', 'b', 'C', 'd', 'E'};
auto it = lower_bound(v.begin(), v.end(), 'C');
cout << "First element which is greater than \'C\' is " << *it << endl;
it = lower_bound(v.begin(), v.end(), 'C', ignore_case);
cout << "First element which is greater than \'C\' is " << *it << endl;
it = lower_bound(v.begin(), v.end(), 'z', ignore_case);
cout << "All elements are less than \'z\'." << endl;
return 0;
}
演示 here .
关于c++ - 无法传递 lower_bound 第四个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54123612/