find_first_of
在目标范围中搜索若干值中任意值的第一个匹配项,或搜索在二元谓词指定的意义上等效于指定元素集的若干元素中任意元素的第一个匹配项。
template<class ForwardIterator1, class ForwardIterator2> ForwardIterator1 find_first_of( ForwardIterator1 _First1, ForwardIterator1 _Last1, ForwardIterator2 _First2, ForwardIterator2 _Last2 ); template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 find_first_of( ForwardIterator1 _First1, ForwardIterator1 _Last1, ForwardIterator2 _First2, ForwardIterator2 _Last2, BinaryPredicate _Comp );
参数
_First1
用于确定要搜索范围中第一个元素的位置的前向迭代器。_Last1
用于确定要搜索范围中最后元素之后下一个元素的位置的前向迭代器。_First2
用于确定要匹配范围中的第一个元素的位置的前向迭代器。_Last2
用于确定要匹配范围中的最后元素之后的位置的前向迭代器。_Comp
用于定义两个元素被视为等效时应满足的条件的用户定义谓词函数对象。 二元谓词采用两个参数并在条件满足时返回 true ,不满足时返回 false。
返回值
用于确定第一个子序列的第一个元素的位置的前向迭代器,这个子序列与指定序列匹配或在二元谓词所指定的某个条件下等效。
备注
operator== 用于确定元素与指定值之间的匹配必须在其操作数之间施加等效关系。
引用的范围必须是有效的;所有指针必须是可解除引用的,并且在每个序列内,最后一个位置可从第一个位置通过递增到达。
示例
// alg_find_first_of.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
// Return whether second element is twice the first
bool twice ( int elem1, int elem2 )
{
return 2 * elem1 == elem2;
}
int main( )
{
using namespace std;
vector <int> v1, v2;
list <int> L1;
vector <int>::iterator Iter1, Iter2;
list <int>::iterator L1_Iter, L1_inIter;
int i;
for ( i = 0 ; i <= 5 ; i++ )
{
v1.push_back( 5 * i );
}
for ( i = 0 ; i <= 5 ; i++ )
{
v1.push_back( 5 * i );
}
int ii;
for ( ii = 3 ; ii <= 4 ; ii++ )
{
L1.push_back( 5 * ii );
}
int iii;
for ( iii = 2 ; iii <= 4 ; iii++ )
{
v2.push_back( 10 * iii );
}
cout << "Vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
cout << "List L1 = ( " ;
for ( L1_Iter = L1.begin( ) ; L1_Iter!= L1.end( ) ; L1_Iter++ )
cout << *L1_Iter << " ";
cout << ")" << endl;
cout << "Vector v2 = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")" << endl;
// Searching v1 for first match to L1 under identity
vector <int>::iterator result1;
result1 = find_first_of ( v1.begin( ), v1.end( ), L1.begin( ), L1.end( ) );
if ( result1 == v1.end( ) )
cout << "There is no match of L1 in v1."
<< endl;
else
cout << "There is at least one match of L1 in v1"
<< "\n and the first one begins at "
<< "position "<< result1 - v1.begin( ) << "." << endl;
// Searching v1 for a match to L1 under the binary predicate twice
vector <int>::iterator result2;
result2 = find_first_of ( v1.begin( ), v1.end( ), v2.begin( ), v2.end( ), twice );
if ( result2 == v1.end( ) )
cout << "There is no match of L1 in v1."
<< endl;
else
cout << "There is a sequence of elements in v1 that "
<< "are equivalent\n to those in v2 under the binary "
<< "predicate twice\n and the first one begins at position "
<< result2 - v1.begin( ) << "." << endl;
}
要求
标头:<algorithm>
命名空间: std