c++ - 无法传递 lower_bound 第四个参数

标签 c++ function 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/

相关文章:

algorithm - 我怎样才能证明下界是\Omega{(n (logn)^k)} ? [k>1]

c++ - 将下界与对象指针的 vector 一起使用

c++ - 是否可以为具有可变参数的宏定义 c++ 包装函数?

ios - 使用全局函数时启动后立即崩溃

c++ - 推送到作为函数参数传递的队列

c++ - 函数不返回值 (C++)

javascript - boolean 值不是函数

c++ - C++二分查找中的比较仿函数

c++ - 任何人都可以向我解释这些陈述背后的逻辑是什么

c++ - 有多少并发的 rtsp 流可以通过 WAN 可靠地直播 555 流