算法库-二分查找操作


c++20
定义于头文件 < algorithm >

lower-bound — 返回指向第一个不小于给定值的元素的迭代器(>= x)

template< class ForwardIt, class T, class Compare >
constexpr ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );
(C++20 起)

返回指向范围 [first, last) 中首个不小于(即大于或等于) value 的元素的迭代器,或若找不到这种元素则返回 last 。

#include<iostream>
#include <algorithm>
#include<vector>
using namespace std;
int main() {
    vector<int> data={2, 3, 4, 6, 7, 8, 10};
    for (int i : data) {
        cout << i << " ";
    }
    cout << endl;
    for (int i = 0; i < 12; ++i) {
        auto lower = lower_bound(data.begin(), data.end(), i);
        cout << i << "<=";
        if (lower != data.end()) {
            cout << *lower << " at index " << distance(data.begin(), lower);
        } else  {
            cout << "not found";
        }
        cout << endl;
    }

    return 0;
}
2 3 4 6 7 8 10 
0<=2 at index 0
1<=2 at index 0
2<=2 at index 0
3<=3 at index 1
4<=4 at index 2
5<=6 at index 3
6<=6 at index 3
7<=7 at index 4
8<=8 at index 5
9<=10 at index 6
10<=10 at index 6
11<=not found

upper_bound — 返回指向第一个大于给定值的元素的迭代器( > x)

template< class ForwardIt, class T, class Compare >
constexpr ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );

#include<iostream>
#include <algorithm>
#include<vector>
using namespace std;
int main() {
    vector<int> data={2, 3, 4, 6, 6, 7, 8, 10};
    for (int i : data) {
        cout << i << " ";
    }
    cout << endl;
    auto iter = lower_bound(data.begin(), data.end(), 6);
    cout << "first num >= 6 " << endl;
    cout << "num : " <<  *iter << " loc : " << distance(data.begin(), iter) << endl <<  endl;
    cout << "last < 6 " << endl;
    if (iter == data.begin()) {
        cout << "not found" << endl;
    } else {
    cout << "num : " << *(iter - 1) << " loc :" << distance(data.begin(), iter) << endl << endl;
    }
    iter = upper_bound(data.begin(), data.end(), 6);
    cout << "first num > 6" << endl;
    cout << "num : " << *iter << " loc : " << distance(data.begin(), iter) << endl << endl;
    
    cout << "last <= 6" << endl;
    iter -= 1;
    cout << "num: " << *iter << " loc : " << distance(data.begin(), iter) << endl << endl;

    return 0;
}

2 3 4 6 6 7 8 10 
first num >= 6 
num : 6 loc : 3

last < 6 
num : 4 loc :3

first num > 6
num : 7 loc : 5

last <= 6
num: 6 loc : 4

binary_search — 确定元素是否存在于某范围中

检查等价于 value 的元素是否出现于范围 [first, last) 中。

对于要成功的 std::binary_search ,范围 [first, last) 必须至少相对于 value 部分有序.

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<string>
using namespace std;
int main() {
    vector<int> haystack{1, 3, 4, 5, 9};
    vector<int> needles{1, 2, 3};
    
    for (auto i : haystack) {
        cout << i << " ";
    }
    cout << endl;

    for (auto needle : needles) {
        cout << "Searching for " << needle << endl;
        if (binary_search(haystack.begin(), haystack.end(), needle)) {
            cout << "Found " << needle << endl;
        } else {
            cout << "no dice" << endl;
        }
    }

    return 0;
}
1 3 4 5 9 
Searching for 1
Found 1
Searching for 2
no dice
Searching for 3
Found 3

equal_range — 返回匹配特定键值的元素范围

template< class ForwardIt, class T, class Compare >
constexpr std::pair<ForwardIt,ForwardIt>
equal_range( ForwardIt first, ForwardIt last, const T& value, Compare comp );

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值