search_n
搜尋項目指定的數字具有特定的值或與該值依二進位述詞的範圍的第一 subsequence。
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
順向迭代器,定址要搜尋之範圍中第一個項目的位置。_Last1
順向迭代器,定址要搜尋之範圍中越過最後一個項目的位置。_Count
subsequence 的大小進行搜尋。_Val
項目的值在序列中進行搜尋。_Comp
使用者定義的述詞函式物件定義將內容的情況下,如果兩個項目要採用視為相等。 符合時,二進位述詞會採用兩個引數並傳回 true ,不符合時則傳回 false。
傳回值
解決向前 Iterator 的第一 subsequence 的第一個項目位置二進位述詞實際上就相當於指定的比對指定的序列或。
備註
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