共用方式為


find (<algorithm>)

找出項目的第一次出現的位置具有指定值的範圍。

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

參數

  • _First
    處理輸入的 Iterator 第一個項目的位置會指定要搜尋的範圍。

  • _Last
    處理輸入的 Iterator 超過最後一個項目的位置是在指定要搜尋的範圍。

  • _Val
    值會搜尋。

傳回值

解決輸入的 Iterator 指定值的第一個符合項目中搜尋範圍的。 如果這個值不存在於這個範圍, Iterator 傳回位址範圍中的最後一個位置,是透過最後一個項目。

備註

用來 operator== 判斷在項目和指定值之間的比對必須強制在其運算元之間的一個層級的關聯性。

範例

// 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.

需求

標題: <algorithm>

命名空間: std

請參閱

參考

find (STL Samples)

標準樣板程式庫