1. 程式人生 > >c++ bind1st 和 bind2nd的用法

c++ bind1st 和 bind2nd的用法

less oid pre 使用方式 output template first span 包含

c++ bind1st 和 bind2nd的用法

來源: http://www.cnblogs.com/renyuan/p/6216375.html

std::bind1st 和 std::bind2nd將二元函數轉換為一元函數,具體用法參加下面的代碼。

代碼介紹了兩種使用方式,第一種是使用std::less和std::greater,第二種是使用自定義的仿函數。

#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
 
 
/**  
* std::bind1st  std::bind2nd 就是將一個二元函數的一個參數設置為定值,這樣二元函數就轉換為了一元函數
* 因為有些算法的參數要求必須是一元函數,但是我們又想用二元函數,那麽就可以使用這兩個函數
*/
/**
*@brief std::less 仿函數的內部實現
    template <class T> struct less : binary_function <T,T,bool> {
        bool operator() (const T& x, const T& y) const {return x<y;}
    };
*/
 
struct person{
    int age;
    std::string name;
};
 
struct person_filter_func: public std::binary_function<person,std::string,bool>
{
    bool operator()(const person& p,const std::string& key) const{
        return (p.name.find(key) != std::string::npos); 
    }
};
 
void disp(int val){    std::cout<<val<<std::endl; }
void disp_v(const person& p){    std::cout<<p.age<<","<<p.name<<std::endl; }
 
int main()
{
    //使用 std::less 仿函數
    int arr[] = {1,2,3,4,5,6,7,8,9};
    std::vector<int> vec;
    std::copy_if(std::begin(arr),std::end(arr),std::back_inserter(vec),std::bind1st(std::less<int>(),6)); //將6 綁定為第一個參數,即 6 < value
    std::for_each(vec.begin(),vec.end(),disp);  // 7 8 9
 
    vec.clear();
    std::copy_if(std::begin(arr),std::end(arr),std::back_inserter(vec),std::bind2nd(std::less<int>(),6)); //將6 綁定為第二個參數,即 value < 6
    std::for_each(vec.begin(),vec.end(),disp); //1 2 3 4 5
 
 
    //使用自定義的仿函數
    std::vector<person> vecP;
    person p1 = {1,"jack"}; vecP.push_back(p1);
    person p2 = {2,"rose"}; vecP.push_back(p2);
    person p3 = {3,"jane"}; vecP.push_back(p3);
 
    std::vector<person> vecRet;
    std::copy_if(vecP.begin(),vecP.end(),std::back_inserter(vecRet),std::bind2nd(person_filter_func(),"ja"));  //將包含關鍵字"ja"的person,復制到vecRet容器中
    std::for_each(vecRet.begin(),vecRet.end(),disp_v);//1, jack  3, jane
}

copy_if:

template <class InputIterator, class OutputIterator, class UnaryPredicate>
  OutputIterator copy_if (InputIterator first, InputIterator last,
                          OutputIterator result, UnaryPredicate pred)
{
  while (first!=last) {
    if (pred(*first)) {
      *result = *first;
      ++result;
    }
    ++first;
  }
  return result;
}

std::bind1st

template <class Operation, class T>
  binder1st<Operation> bind1st (const Operation& op, const T& x)
{
  return binder1st<Operation>(op, typename Operation::first_argument_type(x));
}

std::binder1st

template <class Operation> class binder1st
  : public unary_function <typename Operation::second_argument_type,
                           typename Operation::result_type>
{
protected:
  Operation op;
  typename Operation::first_argument_type value;
public:
  binder1st ( const Operation& x,
              const typename Operation::first_argument_type& y) : op (x), value(y) {}
  typename Operation::result_type
    operator() (const typename Operation::second_argument_type& x) const
    { return op(value,x); }
};

std::remove_if

template <class ForwardIterator, class UnaryPredicate>
  ForwardIterator remove_if (ForwardIterator first, ForwardIterator last,
                             UnaryPredicate pred)
{
  ForwardIterator result = first;
  while (first!=last) {
    if (!pred(*first)) {
      *result = std::move(*first);
      ++result;
    }
    ++first;
  }
  return result;
}

c++ bind1st 和 bind2nd的用法