毋庸置疑,他返回的就是第一个大于等于要搜索的那个数;
但是如果这个数组里面的数都小于或者都大于呢?
本人测试了一下,当数列里面的数都大于这个数,返回0;
当数列里面的数都小于这个数,返回数组上界,(返回N)
以上结论对于(0~n-1)还是(1-n)都是一样
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
int a[100010],b[100010],c[100010];
vector<P>vec;
vector<int>ans;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
for(int i=0;i<m;i++)
scanf("%d",&b[i]);
sort(b,b+m);
for(int i=0;i<n;i++)
{
int pos=lower_bound(b,b+m,a[i])-b;
printf("pos[%d]]=%d\n",i,pos);
}
return 0;
}
输入
3 3
4 5 6
1 2 3
pos[0]]=3
pos[1]]=3
pos[2]]=3
3 3
1 2 3
4 5 6
pos[0]]=0
pos[1]]=0
pos[2]]=0