lower_bound,upper_bound介绍

这两个函数使用前提是,容器内元素是有序的。

lower_bound

  1. Returns an iterator pointing to the first element that is not less than key.
  2. Returns an iterator pointing to the first element that compares not less to the value x. This overload only participates in overload resolution if the qualified-id Compare::is_transparent is valid and denotes a type. They allow calling this function without constructing an instance of Key.

Return value

Iterator pointing to the first element that is not less than key. If no such element is found, a past-the-end iterator (see end()) is returned.

模版:

lower_bound(vec.begin(),vec.end(),x);
	//返回第一个不小于x的迭代器
lower_bound(vec.begin(),vec.end(),x)-vec;
	//返回第一个不小于x的下标

如果没有则返回end()

int a[10] = {2,3,4,5,6,7,8,9,10,11};
int s = lower_bound(a,a+10,4)-a;
cout<<s<<endl; //返回下标2,因为4的下标为2
auto *it = lower_bound(a,a+10,5);
cout<<*it<<endl; //返回值5

upper_bound

  1. Returns an iterator pointing to the first element that is greater than key.
  2. Returns an iterator pointing to the first element that compares greater to the value x. This overload only participates in overload resolution if the qualified-id Compare::is_transparent is valid and denotes a type. They allow calling this function without constructing an instance of Key.

Return value

Iterator pointing to the first element that is greater than key. If no such element is found, past-the-end (see end()) iterator is returned.

模版

upper_bound(vec.begin(),vec.end(),x);
	//返回第一个大于x的迭代器
upper_bound(vec.begin(),vec.end(),x)-vec;
	//返回第一个大于x的元素的下标
int a[10] = {2,3,4,5,6,7,8,9,10,11};
int s = upper_bound(a,a+10,4)-a;
cout<<s<<endl; //返回下标3,因为5是第一个大于4的元素
auto *it = upper_bound(a,a+10,5); 
cout<<*it<<endl;//返回值6,因为6是第一个大于5的元素
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
lower_boundupper_bound是C++中的函数,用于在有序数组中查找特定元素的位置。 lower_bound函数返回大于等于给定值的第一个元素的位置,而upper_bound函数返回大于给定值的第一个元素的位置。 在使用lower_boundupper_bound函数之前,需要确保数组已经按升序排序。这两个函数的使用方法如下: ```cpp int* lower_bound(int* first, int* last, const int& val); int* upper_bound(int* first, int* last, const int& val); ``` 其中,`first`和`last`指定了要查找的数组的范围,`val`是要查找的值。函数返回的是一个指针,指向数组中的位置。 下面是一个示例代码,展示了lower_boundupper_bound函数的使用: ```cpp #include <iostream> #include <algorithm> using namespace std; int main() { const int n = 6; int arr[n]{0, 1, 5, 8, 10, 11}; int* i_lower = lower_bound(arr, arr + n, 8); int* i_upper = upper_bound(arr, arr + n, 8); cout << "i_lower: " << i_lower - arr << endl; // i_lower: 3 cout << "i_upper: " << i_upper - arr << endl; // i_upper: 4 int* j_lower = lower_bound(arr, arr + n, 10); int* j_upper = upper_bound(arr, arr + n, 10); cout << "j_lower: " << j_lower - arr << endl; // j_lower: 4 cout << "j_upper: " << j_upper - arr << endl; // j_upper: 5 return 0; } ``` 在这个例子中,我们定义了一个有序数组arr,并使用lower_boundupper_bound函数查找了值为8和10的元素的位置。输出结果表明,8在数组中的位置范围是<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [lower_bound()与upper_bound()](https://blog.csdn.net/Boruson/article/details/125702597)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值