关于lower_bound与upper_bound

介绍:

ps:stl容器中也有这两个函数,在这里就不讲了,具体详情参考老师的课件。

  1. lower_bound( )是用来查找一个数组中第一个大于等于一个数的函数,返回值是这个数的指针。
  2. upper_bound( )是用来查找一个数组中第一个大于一个数的函数,返回值是这个数的指针。

ps:如果你强行把这两个函数的值用%d输出,那么你就可以得到函数找到的数的下标,只是编译时编译器会警告你罢了。

用法:

int arr[10005];
cout<<*upper_bound(arr+1,arr+1+100,13)<<endl;
cout<<*lower_bound(arr+1,arr+1+100,13)<<endl;
cout<<upper_bound(arr+1,arr+1+100,13)<<endl; //编译时会警告
cout<<lower_bound(arr+1,arr+1+100,13)<<endl; //编译时会警告

猜你喜欢

转载自www.cnblogs.com/lixiao189/p/9301027.html
今日推荐