lower_bound
在包含的值大於或等於指定值的已排序範圍中尋找第一個項目的位置,其中排序準則可藉由二元述詞指定。
template<class ForwardIterator, class Type>
ForwardIterator lower_bound(
ForwardIterator first,
ForwardIterator last,
const Type& value
);
template<class ForwardIterator, class Type, class BinaryPredicate>
ForwardIterator lower_bound(
ForwardIterator first,
ForwardIterator last,
const Type& value,
BinaryPredicate comp
);
參數
first
順向迭代器,定址要搜尋之範圍中第一個項目的位置。last
順向迭代器,定址要搜尋之範圍中越過最後一個項目的位置。value
排序的範圍中,第一個位置或第一個可能的位置中搜尋的值。comp
使用者定義的述詞函式物件,定義在此意義上某個項目小於另一個。 符合時,二進位述詞會採用兩個引數並傳回 true ,不符合時則傳回 false。
傳回值
在包含的值大於或等於指定值的已排序範圍中,在第一個項目的位置的順向迭代器,其中相等準則可藉由二元述詞指定。
備註
參考的已排序來源範圍必須是有效的。所有迭代器都必須是可取值的,而且序列中的最後一個位置必須可從第一個位置透過遞增方式到達。
排序的範圍是使用 lower_bound 的前置條件,而且排序準則與二進位述詞所指定的準則相同。
lower_bound 演算法不會修改這個範圍。
向前迭代器的實值類型必須是小於比較才能排序,因此若提供了兩個項目,可以判斷它們相等 (任一個都不小於另一個的意義),或者一個小於另一個。 這會導致在非對等元件之間的排序
演算法的複雜度為隨機存取迭代器的對數,若步驟數目成比例則為線性 (last – first)。
範例
// alg_lower_bound.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional> // 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;
// Constructing vector v1 with default less-than ordering
for ( auto i = -1 ; i <= 4 ; ++i )
{
v1.push_back( i );
}
for ( auto ii =-3 ; ii <= 0 ; ++ii )
{
v1.push_back( ii );
}
cout << "Starting vector v1 = ( " ;
for (const auto &Iter : v1)
cout << Iter << " ";
cout << ")." << endl;
sort(v1.begin(), v1.end());
cout << "Original vector v1 with range sorted by the\n "
<< "binary predicate less than is v1 = ( " ;
for (const auto &Iter : v1)
cout << Iter << " ";
cout << ")." << endl;
// Constructing vector v2 with range sorted by greater
vector<int> v2(v1);
sort(v2.begin(), v2.end(), greater<int>());
cout << "Original vector v2 with range sorted by the\n "
<< "binary predicate greater is v2 = ( " ;
for (const auto &Iter : v2)
cout << Iter << " ";
cout << ")." << endl;
// Constructing vectors v3 with range sorted by mod_lesser
vector<int> v3(v1);
sort(v3.begin(), v3.end(), mod_lesser);
cout << "Original vector v3 with range sorted by the\n "
<< "binary predicate mod_lesser is v3 = ( " ;
for (const auto &Iter : v3)
cout << Iter << " ";
cout << ")." << endl;
// Demonstrate lower_bound
vector<int>::iterator Result;
// lower_bound of 3 in v1 with default binary predicate less<int>()
Result = lower_bound(v1.begin(), v1.end(), 3);
cout << "The lower_bound in v1 for the element with a value of 3 is: "
<< *Result << "." << endl;
// lower_bound of 3 in v2 with the binary predicate greater<int>( )
Result = lower_bound(v2.begin(), v2.end(), 3, greater<int>());
cout << "The lower_bound in v2 for the element with a value of 3 is: "
<< *Result << "." << endl;
// lower_bound of 3 in v3 with the binary predicate mod_lesser
Result = lower_bound(v3.begin(), v3.end(), 3, mod_lesser);
cout << "The lower_bound in v3 for the element with a value of 3 is: "
<< *Result << "." << endl;
}
Output
Starting vector v1 = ( -1 0 1 2 3 4 -3 -2 -1 0 ).
Original vector v1 with range sorted by the
binary predicate less than is v1 = ( -3 -2 -1 -1 0 0 1 2 3 4 ).
Original vector v2 with range sorted by the
binary predicate greater is v2 = ( 4 3 2 1 0 0 -1 -1 -2 -3 ).
Original vector v3 with range sorted by the
binary predicate mod_lesser is v3 = ( 0 0 -1 -1 1 -2 2 -3 3 4 ).
The lower_bound in v1 for the element with a value of 3 is: 3.
The lower_bound in v2 for the element with a value of 3 is: 3.
The lower_bound in v3 for the element with a value of 3 is: -3.
需求
標頭:<algorithm>
命名空間: std