跳到正文

个人记录

二分函数

wenyimos约 1 分钟
原文
文章信息与版本

文章信息

发布
2026年8月25日
收录
2026年11月8日
更新
2026年11月15日
篇幅
约 1 分钟

版本

  1. v1当前
C++
sort(a+1,a+n+1);//排序 
int it=lower_bound(a+1,a+n+1,x)-a;//要在a[1]~a[n]里找第一个大于等于x的位置
int it=upper_bound(a+1,a+n+1,x)-a;//upper_bound找的是第一个严格大于x的位置
int it=lower_bound(a+1,a+n+1,x);cout<<*it;//要在a[1]~a[n]里找第一个大于等于x的值 
//在使用lower_bound和upper_bound都需要保证容器有序(从小到大排列)
vector<int>G;//构造一个叫G的容器 
G.push_back(2);
sort(G.begin(),G.end());
auto it=lower_bound(G.begin(),G.end(),x);//用法同上 
原文