首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::upper_bound

Defined in header <algorithm>

?

?

template< class ForwardIt, class T > ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value );

(1)

?

template< class ForwardIt, class T, class Compare > ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );

(2)

?

返回指向范围中的第一个元素的迭代器。[first, last)那就是更大value...

范围[first, last)必须至少是部分有序的,即对表达式进行分区。!(value < element)!comp(value, element)完全排序的范围符合此标准,调用std::partition...

第一个版本使用operator<为了比较元素,第二个版本使用给定的比较函数。comp...

参数

first, last

-

the range of elements to examine

value

-

value to compare the elements to

comp

-

comparison function object (i.e. an object that satisfies the requirements of Compare) which returns ?true if the first argument is less than the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function object must not modify the objects passed to it. The type Type1 must be such that an object of type T can be implicitly converted to Type1. The type Type2 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to Type2. ?

类型要求

---。

返回值

迭代器指向第一个元素,即更大value,或last如果找不到这样的元素。

复杂性

执行的比较数为对数,其距离为firstlast%28%,大部分原木

2%28最后-首%29+O%281%29比较%29。但是,对于非-RandomAccessIterator斯,迭代器增量的数目是线性的。

可能的实施

第一版

*。

模板<类向前,类T>向前向上[医]绑定%28 Forwardit First,Forwardit Lest,Const T&value%29{Forwardit;type Name std::iterator[医]性状<ForwardIt>*差异[医]类型计数,步骤;计数=std::距离%281,最后%29;而%28计数>0%29{it=先;步骤=计数/2;std:前进%28 it,步骤%29;if%28%21%28值<%2A它%29%29{First=++it;count-=Step+1;}or count=Step;}返回第一步;}

第二版

模板<类向前,类T,类比较>[医]绑定%28 ForwardIt First,Forwardit Lest,Const T&Value,比较comp%29{Forwardit;type Name std::iterator[医]性状<ForwardIt>*差异[医]类型计数,步骤;计数=std::距离%281,最后%29;而%28计数>0%29{it=先;STEP=计数/2;std:前进%28 it,步骤%29;如果%28%21 comp%28值,%2A它%29%29{First=++it;count-=Step+1;}or count=Step;}返回第一步;}

二次

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
 
int main()
{
    std::vector<int> data = { 1, 1, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6 };
 
    auto lower = std::lower_bound(data.begin(), data.end(), 4);
    auto upper = std::upper_bound(data.begin(), data.end(), 4);
 
    std::copy(lower, upper, std::ostream_iterator<int>(std::cout, " "));
}

二次

产出:

二次

代码语言:javascript
复制
4 4 4

二次

另见

equal_range

returns range of elements matching a specific key (function template)

lower_bound

returns an iterator to the first element not less than the given value (function template)

partition

divides a range of elements into two groups (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com