lower_bound
查找第一个元素的位置在具有值大于或等效于指定值,该排序的标准可以由二进制谓词指定的有序的大小。
template<class ForwardIterator, class Type>
ForwardIterator lower_bound(
ForwardIterator _First,
ForwardIterator _Last,
const Type& _Val
);
template<class ForwardIterator, class Type, class BinaryPredicate>
ForwardIterator lower_bound(
ForwardIterator _First,
ForwardIterator _Last,
const Type& _Val,
BinaryPredicate _Comp
);
参数
_First
解决仅向前迭代器的第一个元素的位置在要搜索的范围。_Last
解决仅向前的迭代器通过最终元素的位置一在要搜索的范围。_Val
第一个位置或可能的第一个位置中搜索该顺序的范围内的值。_Comp
定义含义的用户定义的谓词函数对象哪个元素比另一个小于。 二进制谓词采用两个参数并返回 true ,在满足和 false,在未满足。
返回值
仅向前迭代器在第一个元素的位置与大于或等效于指定值的值的有序的大小,它们指定是二进制性质。
备注
引用的排序的源范围必须是有效的;所有迭代器必须dereferenceable,并在该序列中最后位置必须是可访问的从开始按增量。
已排序的范围是使用 lower_bound 的前置条件,以及顺序与指定是二进制谓词的位置。
算法不修改该范围 lower_bound。
向前迭代的值类型需要小于可将排序,因此,命名两个组件,可以确定为它们是等效的(来讲都比其他不小于)或一个比其他小于。 这将导致排序在非等价的组件间
算法的复杂否则是对数为随机访问迭代器和线性,与步骤数比例为(_Last1 – _First1)。
示例
// alg_lower_bound.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional> // For greater<int>( )
#include <iostream>
// Return whether modulus of elem1 is less than modulus of elem2
bool mod_lesser ( int elem1, int elem2 )
{
if ( elem1 < 0 )
elem1 = - elem1;
if ( elem2 < 0 )
elem2 = - elem2;
return elem1 < elem2;
}
int main( )
{
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1, Result1;
// Constructing vectors v1a & v1b with default less than ordering
int i;
for ( i = -1 ; i <= 4 ; i++ )
{
v1.push_back( i );
}
int ii;
for ( ii =-3 ; ii <= 0 ; ii++ )
{
v1.push_back( ii );
}
sort ( v1.begin ( ) , v1.end ( ) );
cout << "Original vector v1 with range sorted by the\n "
<< "binary predicate less than is v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
// Constructing vectors v2 with range sorted by greater
vector <int> v2 ( v1 );
vector <int>::iterator Iter2, Result2;
sort ( v2.begin ( ) , v2.end ( ) , greater<int> ( ) );
cout << "Original vector v2 with range sorted by the\n "
<< "binary predicate greater is v2 = ( " ;
for ( Iter2 = v2.begin ( ) ; Iter2 != v2.end ( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")." << endl;
// Constructing vectors v3 with range sorted by mod_lesser
vector <int> v3 ( v1 );
vector <int>::iterator Iter3, Result3;
sort ( v3.begin ( ) , v3.end ( ) , mod_lesser );
cout << "Original vector v3 with range sorted by the\n "
<< "binary predicate mod_lesser is v3 = ( " ;
for ( Iter3 = v3.begin ( ) ; Iter3 != v3.end ( ) ; Iter3++ )
cout << *Iter3 << " ";
cout << ")." << endl;
// lower_bound of 3 in v1 with default binary predicate less <int> ( )
Result1 = lower_bound ( v1.begin ( ) , v1.end ( ) , 3 );
cout << "The lower_bound in v2 for the element with a value of 3 is: "
<< *Result1 << "." << endl;
// lower_bound of 3 in v2 with the binary predicate greater <int> ( )
Result2 = lower_bound ( v2.begin ( ) , v2.end ( ) , 3, greater <int> ( ) );
cout << "The lower_bound in v2 for the element with a value of 3 is: "
<< *Result2 << "." << endl;
// lower_bound of 3 in v3 with the binary predicate mod_lesser
Result3 = lower_bound ( v3.begin ( ) , v3.end ( ) , 3, mod_lesser );
cout << "The lower_bound in v3 for the element with a value of 3 is: "
<< *Result3 << "." << endl;
}
要求
标头: <algorithm>
命名空间: std