C++ 二分查找函数lower_bound() 和 upper_bound()

lower_bound() 和 upper_bound() 是 C++ 标准库中的函数,用于在有序序列中查找目标值的插入位置。

lower_bound() 函数:

lower_bound() 函数返回一个迭代器,指向第一个不小于目标值的元素位置。

template<class ForwardIt, class T>
ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value);
  • firstlast :表示要搜索的有序序列的范围,[first, last) 是一个左闭右开区间。
  • value :要查找的目标值。

lower_bound() 函数的返回值是一个迭代器,指向序列中第一个不小于目标值的元素的位置。如果序列中不存在不小于目标值的元素,则返回迭代器指向的位置为序列的末尾(即 last)。

upper_bound() 函数:

upper_bound() 函数返回一个迭代器,指向第一个大于目标值的元素位置。

template<class ForwardIt, class T>
ForwardIt upper_bound(ForwardIt first, ForwardIt last, const T& value);
  • firstlast :表示要搜索的有序序列的范围,[first, last) 是一个左闭右开区间。
  • value :要查找的目标值。

upper_bound() 函数的返回值是一个迭代器,指向序列中第一个大于目标值的元素的位置。如果序列中不存在大于目标值的元素,则返回迭代器指向的位置为序列的末尾(即 last)。

C++ 代码示例:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int> nums = {1, 2, 3, 4, 4, 5, 6, 7};
    int target = 4;

    // 使用 lower_bound() 函数查找插入位置
    auto lb = lower_bound(nums.begin(), nums.end(), target);
    cout << "第一个不小于目标值4的元素的位置: " << distance(nums.begin(), lb) << endl;

    // 使用 upper_bound() 函数查找插入位置
    auto ub = upper_bound(nums.begin(), nums.end(), target);
    cout << "第一个大于目标值4的元素的位置: " << distance(nums.begin(), ub) << endl;

    return 0;
}

在这里插入图片描述

以上代码演示了如何使用 lower_bound() 和 upper_bound() 函数在有序序列中查找目标值的插入位置。给定一个有序序列 nums 和目标值 target,通过 lower_bound() 和 upper_bound() 函数找到第一个不小于目标值的位置和第一个大于目标值的位置。

希望这个解释和代码示例能够帮助你理解 lower_bound() 和 upper_bound() 函数的原理和使用方法。

  • 25
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
所有的 C / C++ 函数 Constructors (cppstring) Constructors (cppvector) Operators (cppbitset) Operators (cppdeque) Operators (cppstack) Operators (cppstring) Operators (cppvector) abort (stdother) abs (stdmath) acos (stdmath) any (cppbitset) append (cppstring) asctime (stddate) asin (stdmath) assert (stdother) assign (cppdeque) assign (cpplist) assign (cppstring) assign (cppvector) at (cppdeque) at (cppstring) at (cppvector) atan (stdmath) atan2 (stdmath) atexit (stdother) atof (stdstring) atoi (stdstring) atol (stdstring) back (cppdeque) back (cpplist) back (cppqueue) back (cppvector) bad (cppio) begin (cppdeque) begin (cpplist) begin (cppmap) begin (cppmultimap) begin (cppmultiset) begin (cppset) begin (cppstring) begin (cppvector) bsearch (stdother) c_str (cppstring) calloc (stdmem) capacity (cppstring) capacity (cppvector) ceil (stdmath) clear (cppdeque) clear (cppio) clear (cpplist) clear (cppmap) clear (cppmultimap) clear (cppmultiset) clear (cppset) clear (cppvector) clearerr (stdio) clock (stddate) compare (cppstring) copy (cppstring) cos (stdmath) cosh (stdmath) count (cppbitset) count (cppmap) count (cppmultimap) count (cppmultiset) count (cppset) ctime (stddate) data (cppstring) #define (preproc) difftime (stddate) div (stdmath) empty (cppdeque) empty (cpplist) empty (cppmap) empty (cppmultimap) empty (cppmultiset) empty (cpppriorityqueue) empty (cppqueue) empty (cppset) empty (cppstack) empty (cppstring) empty (cppvector) end (cppdeque) end (cpplist) end (cppmap) end (cppmultimap) end (cppmultiset) end (cppset) end (cppstring) end (cppvector) eof (cppio) equal_range (cppmap) equal_range (cppmultimap) equal_range (cppmultiset) equal_range (cppset) erase (cppdeque) erase (cpplist) erase (cppmap) erase (cppmultimap) erase (cppmultiset) erase (cppset) erase (cppstring) erase (cppvector) #error (preproc) exit (stdother) exp (stdmath) fabs (stdmath) fail (cppio)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值