upper_bound
尋找第一個項目的位置是值大於指定值大於,排序準則可能是由二進位述詞指定的已排序的範圍。
template<class ForwardIterator, class Type>
ForwardIterator upper_bound(
ForwardIterator _First,
ForwardIterator _Last,
const Type& _Val
);
template<class ForwardIterator, class Type, class Predicate>
ForwardIterator upper_bound(
ForwardIterator _First,
ForwardIterator _Last,
const Type& _Val,
Predicate _Comp
);
參數
_First
第一個項目的位置會搜尋的範圍。_Last
超過最後一個項目的位置是在要搜尋的範圍。_Val
在所需的Iterator處理之項目的值超過的已排序範圍內的型別。_Comp
定義感受遠遠之使用者定義的述詞函式物件哪一個項目小於另一個。一個二進位述詞採用兩個引數並傳回 true ,當內容和 false ,則內容。
傳回值
將具有指定值大於的值的第一個項目位置的順向Iterator。
備註
參考的已排序資料來源範圍必須是有效的,所有 Iterator 必須 dereferenceable,並在序列中最後一個位置必須是可取得的開頭會增加。
已排序的範圍都是使用的先決條件, upper_bound 及排程準則與指定由這個二進位述詞的區域。
upper_bound未修改範圍。
正向 iterator 可用於將實值型別必須小於可比較將已排序,則,因此將兩個項目,可以判斷或其相等 (因為都比其他不小於) 或是小於另一個。這會導致排程在非對等的項目。
演算法的複雜則是指定要為隨機存取 Iterator 以線性的方式,與步驟數比例調整 (_Last1 – _First1)。
範例
// alg_upper_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;
// upper_bound of 3 in v1 with default binary predicate less <int> ( )
Result1 = upper_bound ( v1.begin ( ) , v1.end ( ) , 3 );
cout << "The upper_bound in v2 for the element with a value of 3 is: "
<< *Result1 << "." << endl;
// upper_bound of 3 in v2 with the binary predicate greater <int> ( )
Result2 = upper_bound ( v2.begin ( ) , v2.end ( ) , 3, greater <int> ( ) );
cout << "The upper_bound in v2 for the element with a value of 3 is: "
<< *Result2 << "." << endl;
// upper_bound of 3 in v3 with the binary predicate mod_lesser
Result3 = upper_bound ( v3.begin ( ) , v3.end ( ) , 3,mod_lesser );
cout << "The upper_bound in v3 for the element with a value of 3 is: "
<< *Result3 << "." << endl;
}
需求
標題: <algorithm>
命名空間: std