lower_bound中的comp使用方法
定义:
定义有两种,一种有comp函数,一种没有comp函数。
template< class ForwardIt, class T >
constexpr ForwardIt lower_bound( ForwardIt first, ForwardIt last,
const T& value );
template< class ForwardIt, class T, class Compare >
constexpr ForwardIt lower_bound( ForwardIt first, ForwardIt last,
const T& value, Compare comp );
参数:
first,last - 要被查找的有序范围的开始和结束迭代器,查找范围为[first, last)。
value - 被用来比较的值
comp - 比较函数,用于自定义比较规则。该函数的返回值为 true 和 false 其中之一。
bool pred(const Type1 &a, const Type2 &b);
bool pred(const Type1 a, const Type2 b);
其中 Type1 必须是 ForwardIt 指针所指向的对象类型。
返回值:
返回一个迭代器。返回第一个不满足 element < value
或者 comp(element, value)
返回false 的元素的迭代器,如果没有找到这样的 element
则返回 last
。简单的来说,就是返回第一个element>value
和第一个不符合comp(element, value)
规则的元素的指针。也就是将要比较的地址空间内容都传到comp(element, value)
函数中,得到一个[true,true,....true,false,...false]
的数组,返回的就是第一个false
的地址。
源码:
template<class ForwardIt, class T, class Compare>
ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value, Compare comp)
{
ForwardIt it;
typename std::iterator_traits<ForwardIt>::difference_type count, step;
count = std::distance(first, last);
while (count > 0)
{
it = first;
step = count / 2;
std::advance(it, step);
if (comp(*it, value))
{
first = ++it;
count -= step + 1;
}
else
count = step;
}
return first;
}
具体例子
为了进一步理解comp(element, value)
函数,下面我使用了一个无序的数组来进行比较,在实际应用中强烈不建议这么用!
根据源码:distance(first, last) = 6
,第一次step = 3,会将alls[3]
与4比较返回true
,所以此时搜索空间变成[4,6]。第二次会将alls[5]
与4进行比较,返回true
,此时搜索空间变为[6,6]。第三次将alls[6]
与4进行比较,返回true
,此时count<0
,退出循环,返回下标7,也就是alls.end()
。
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> alls{5, 7, 1, 4, 2, 3, 1};
auto it = lower_bound(alls.begin(), alls.end(), 4,
[](const int &element, const int &value)
{
return element <= value;
});
if(it == alls.end()) cout<<"Not find!"<<endl;
else cout << "Position is" << it - alls.begin() << endl;
return 0;
}
运行结果:
Not find!
另一个例子:
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> alls{6, 5, 4, 3, 2, 1, 0};
auto it = lower_bound(alls.begin(), alls.end(), 4,
[](const int &element, const int &value)
{
return element >= value;
});
if(it == alls.end()) cout<<"Not find!"<<endl;
cout << "Position is " << it - alls.begin() << endl;
return 0;
}
运行结果:
Position is 3
总结:对于升序序列,返回值只能写成element <= value
或者element < value
,对于降序序列反之。
最后再附上cppreference里的参考代码:
#include <algorithm>
#include <iostream>
#include <vector>
struct PriceInfo { double price; };
int main()
{
const std::vector<int> data {1, 2, 4, 5, 5, 6};
for (int i = 0; i < 8; ++i)
{
// Search for first element x such that i ≤ x
auto lower = std::lower_bound(data.begin(), data.end(), i);
std::cout << i << " ≤ ";
lower != data.end()
? std::cout << *lower << " at index " << std::distance(data.begin(), lower)
: std::cout << "not found";
std::cout << '\n';
}
std::vector<PriceInfo> prices {{100.0}, {101.5}, {102.5}, {102.5}, {107.3}};
for (double to_find : {102.5, 110.2})
{
auto prc_info = std::lower_bound(prices.begin(), prices.end(), to_find,
[](const PriceInfo& info, double value)
{
return info.price < value;
});
prc_info != prices.end()
? std::cout << prc_info->price << " at index " << prc_info - prices.begin()
: std::cout << to_find << " not found";
std::cout << '\n';
}
}
运行结果:
0 ≤ 1 at index 0
1 ≤ 1 at index 0
2 ≤ 2 at index 1
3 ≤ 4 at index 2
4 ≤ 4 at index 2
5 ≤ 5 at index 3
6 ≤ 6 at index 5
7 ≤ not found
102.5 at index 2
110.2 not found