search_n
搜尋指定的項目數字具有特定的值或關聯至該值所指定一個二進位述詞的範圍中的第一個序列。
template<class ForwardIterator1, class Diff2, class Type>
ForwardIterator1 search_n(
ForwardIterator1 _First1,
ForwardIterator1 _Last1,
Diff2 _Count,
const Type& _Val
);
template<class ForwardIterator1, class Diff2, class Type, class BinaryPredicate>
ForwardIterator1 search_n(
ForwardIterator1 _First1,
ForwardIterator1 _Last1,
Diff2 _Count,
const Type& _Val,
BinaryPredicate _Comp
);
參數
_First1
處理順向 Iterator 的第一個項目的位置會搜尋的範圍。_Last1
處理順向的 Iterator 超過最後一個項目的位置是在要搜尋的範圍。_Count
序列的大小會搜尋。_Val
項目的值序列中的來搜尋。_Comp
定義要滿足條件的使用者定義之述詞函式物件,如果兩個項目將會視為相等。 一個二進位述詞採用兩個引數並傳回 true ,當內容和 false ,則內容。
傳回值
處理順向 Iterator 的第一個序列的第一個項目位置一類的二進位述詞實際上就相當於指定的符合指定的序列或。
備註
用來 operator== 判斷在項目和指定值之間的比對必須強制在其運算元之間的一個層級的關聯性。
參考的範圍必須是有效的,任何指標必須 dereferenceable,並在序列中最後一個位置開始可取得的會增加。
複雜度是線性與所要搜尋的大小。
範例
// alg_search_n.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
// Return whether second element is 1/2 of the first
bool one_half ( int elem1, int elem2 )
{
return elem1 == 2 * elem2;
}
int main( )
{
using namespace std;
vector <int> v1, v2;
vector <int>::iterator Iter1;
int i;
for ( i = 0 ; i <= 5 ; i++ )
{
v1.push_back( 5 * i );
}
for ( i = 0 ; i <= 2 ; i++ )
{
v1.push_back( 5 );
}
for ( i = 0 ; i <= 5 ; i++ )
{
v1.push_back( 5 * i );
}
for ( i = 0 ; i <= 2 ; i++ )
{
v1.push_back( 10 );
}
cout << "Vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
// Searching v1 for first match to (5 5 5) under identity
vector <int>::iterator result1;
result1 = search_n ( v1.begin( ), v1.end( ), 3, 5 );
if ( result1 == v1.end( ) )
cout << "There is no match for a sequence ( 5 5 5 ) in v1."
<< endl;
else
cout << "There is at least one match of a sequence ( 5 5 5 )"
<< "\n in v1 and the first one begins at "
<< "position "<< result1 - v1.begin( ) << "." << endl;
// Searching v1 for first match to (5 5 5) under one_half
vector <int>::iterator result2;
result2 = search_n (v1.begin( ), v1.end( ), 3, 5, one_half );
if ( result2 == v1.end( ) )
cout << "There is no match for a sequence ( 5 5 5 ) in v1"
<< " under the equivalence predicate one_half." << endl;
else
cout << "There is a match of a sequence ( 5 5 5 ) "
<< "under the equivalence\n predicate one_half "
<< "in v1 and the first one begins at "
<< "position "<< result2 - v1.begin( ) << "." << endl;
}
需求
標題: <algorithm>
命名空間: std