find_if()用法

find_if 算法的定義如下:

template<class InputIterator, class T, class Predicate> inline

 

   InputIterator find_if(

 

      InputIterator First,

 

      InputIterator Last,

 

      Predicate Predicate

 

   )

 

  它和find算法很類似返回的是找到元素的佚代器。它的第三個參數是一個函數指針(function pointer)或函數對像(object functional)如果是函數對像,它的定義可以是這樣。

template <class T>

 

class fun

 

{

 

public:

 

bool operator()(const T &a)const

 

{

 

        if(a > 10)

 

               return true;

 

}

 

};

 

這裏實現了一個找出大於30的元素(當然元素不一不是int型的)這裏是一個小例子:

#include <iostream>

 

#include <algorithm>

 

#include <functional>

 

#include <vector>

 

#include "time.h"

 

using namespace std;

 

 

int _rand()

 

{

 

return rand()%100;

 

}

 

 

template <class T>

 

class fun

 

{

 

public:

 

bool operator()(const T &a)const

 

{

 

        return a>30;

 

       

 

}

 

 

};

 

int main(int argc, char* argv[])

 

{

 

srand(time(NULL));

 

vector<int> vec(16);

 

generate(vec.begin(),vec.end(),_rand);

 

copy(vec.begin(),vec.end(),ostream_iterator<int>(cout," "));

 

 

cout << endl; 

 

vector<int>::iterator itr =vec.begin();

 

while(itr != vec.end())

 

{

 

        itr = find_if(itr,vec.end(),fun<int>());

 

        if(itr != vec.end())

 

        {

 

               cout <<*itr << " ";

 

               itr++;

 

        }

 

}

 

return 0;

 

}

 

 

但是,如果我們要比較的數是一個在運行時改變的數,比如,第一次要找大去30的,第二次要找大於35的,這如何時實呢?對,binder2nd()!

它是一個模版類定義在<functional>頭文件裏。

template<class Operation>

 

   class binder2nd

 

      : public unary_function <

 

         typename Operation::first_argument_type,

 

         typename Operation::result_type>

 

   {

 

   public:

 

   typedef typename Operation::first_argument_type argument_type;

 

   typedef typename Operation::result_type result_type;

 

   binder2nd(

 

      const Operation& _Func,

 

      const typename Operation::second_argument_type& _Right

 

   );

 

   result_type operator()(

 

      const argument_type& _Left

 

   ) const;

 

   result_type operator()(

 

   argument_type& _Left

 

   ) const;

 

   protected:

 

   Operation op;

 

   typename Operation::second_argument_type value;

 

   };

 

怎麼樣,看暈了吧?簡單說下_Func是要綁定的函數對象,_Right是要綁定的二原函數的第二個參數。那第一個參數呢?是留給find_if用的。這個函數對象binary_function派生出來的,我們看看看binary_function的定義:

 

 

 

 

 

 

 

它是一個用模版定義的類,用來對函數對象提供C++標準模版庫的支持,first_argument_type 表示第一個參數類型,second_argument_type 表示第二個參數類型result_type表示返回值類型C++庫的函數對象都是從這個類派生出來的。

 

說了這麼多怎麼用它呢:我還是給個例子吧!功能和上面的差不多,找出大於30的數。看好了!

 

#include "stdafx.h"

 

#include <iostream>

 

#include <algorithm>

 

#include <functional>

 

#include <vector>

 

#include "time.h"

 

using namespace std;

 

 

template<class T>

 

class fun: public binary_function<T, T, bool>

 

{

 

public:

 

  result_type operator() ( const first_argument_type a,const second_argument_type b ) const

 

  {

 

         return a>b;

 

  }

 

};

 

 

int _rand()

 

{

 

  return rand()%100;

 

}

 

 

int main(int argc, char* argv[])

 

{

 

  srand(time(NULL));

 

  vector<int> vec(16);

 

  generate(vec.begin(),vec.end(),_rand);

 

  copy(vec.begin(),vec.end(),ostream_iterator<int>(cout," "));

 

  cout << endl; 

 

  vector<int>::iterator itr =vec.begin();

 

  while(itr != vec.end())

 

  {

 

         itr = find_if(itr,vec.end(),binder2nd<fun<int> >(fun<int>(),30));

 

         if(itr != vec.end())

 

         {

 

                cout <<*itr << " ";

 

                itr++;

 

         }

 

  }

 

  return 0;

 

} 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章