Partilhar via


find (<algorithm>)

Locates the position of the first occurrence of an element in a range that has a specified value.

template<class InputIterator, class Type>
   InputIterator find(
      InputIterator _First, 
      InputIterator _Last, 
      const Type& _Val
   );

Parameters

  • _First
    An input iterator addressing the position of the first element in the range to be searched for the specified value.

  • _Last
    An input iterator addressing the position one past the final element in the range to be searched for the specified value.

  • _Val
    The value to be searched for.

Return Value

An input iterator addressing the first occurrence of the specified value in the range being searched. If no such value exists in the range, the iterator returned addresses the last position of the range, one past the final element.

Remarks

The operator== used to determine the match between an element and the specified value must impose an equivalence relation between its operands.

Example

// alg_find.cpp
// compile with: /EHsc 
#include <list>
#include <algorithm>
#include <iostream>

int main() {
   using namespace std;

   list <int> L;
   list <int>::iterator Iter;
   list <int>::iterator result;
   
   L.push_back( 40 );
   L.push_back( 20 );
   L.push_back( 10 );
   L.push_back( 30 );
   L.push_back( 10 );

   cout << "L = ( " ;
   for ( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ )
      cout << *Iter << " ";
   cout << ")" << endl;
   
   result = find( L.begin( ), L.end( ), 10 );
   if  ( result == L.end( ) )
      cout << "There is no 10 in list L.";
   else {
      cout << "There is a 10 in list L";
      if ( ++result != L.end() )
         cout << " and it is followed by a " << *result << ".";
   }
   cout << endl;
}

Output

L = ( 40 20 10 30 10 )
There is a 10 in list L and it is followed by a 30.

Requirements

Header: <algorithm>

Namespace: std

See Also

Reference

find (STL Samples)

Standard Template Library

Other Resources

<algorithm> Members