c++ bind1st 和 bind2nd的用法

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;
}
时间: 2024-08-25 05:15:40

c++ bind1st 和 bind2nd的用法的相关文章

not1,not2,bind1st和bind2nd详解

1.引言 bind1st和bind2nd函数用于将一个二元算子(binary functor,bf)转换成一元算子(unary functor,uf).为了达到这个目的,它们需要两个参数:要转换的bf和一个值(v). 可能这么解释以后大家还不是很清楚,那么就说点白话吧.我们在做比较的时候所写的表达式像 x > k ,x < k,这里的k是一个参数表示你程序里面的表达式要和k值去比较.上面这两个表达式对应的应该是bind2nd ,简单的理解就是把k作为比较表达式的第二个参数.如果使用bind1s

STL源码解析之bind1st和bind2nd

首先我们先来了解一下一元函数和二元函数.一元函数数学上一般形式表示为 z = f(x),只有一个变量x.二元函数数学上一般形式表示为 z = f(x,y),存在两个变量,分别是x和y. STL中为了描述一元函数和二元函数,定义了两个结构体来描述.如下: //一元函数结构 template <class Arg, class Result> struct unary_function { typedef Arg argument_type; //参数类型,可以理解为x对应的类型 typedef

not1,not2,bind1st,bind2nd

例子需要包含头文件 #include <vector> #include <algorithm> #include <functional> bind1st和bind2nd函数用于将一个二元函数对象(binary functor,bf)转换成一元函数对象(unary functor,uf).为了达到这个目的,它们需要两个参数:要转换的bf和一个值(v). 可能这么解释以后大家还不是很清楚,那么就说点白话吧.我们在做比较的时候所写的表达式像 x > k ,x <

c++ bind1st bind2nd的使用

看了下面这篇文章后,简短总结:bind2nd( 参数1,参数2)   表示的是一个判断条件,假如参数1是个小于号<,而参数2是个100,那么表达的就“小于100”的意思,将两个东西结合在一起表示.bind1st( 参数1,参数2)  表达的与bind2nd恰好相反,即假如参数1与参数2与bind2nd完全一样,表达的是“大于100”的意思. 以上只是一总抽象的总结性,你理解了这个大概的意思,再看下面具体的介绍之后比较容易理解. 转载自http://blog.csdn.net/simahao/ar

【转】 bind1st bind2nd的使用

以前在使用stl的过程中发现bind1st和bind2nd这两个函数,当时不太理解什么意思,今天在网上查了一下相关资料发现竟然很简单,下面我就具体解释一下他们的用法. bind1st和bind2nd函数用于将一个二元算子(binary functor,bf)转换成一元算子(unary functor,uf).为了达到这个目的,它们需要两个参数:要转换的bf和一个值(v).   可能这么解释以后大家还不是很清楚,那么就说点白话吧.我们在做比较的时候所写的表达式像 x > k ,x < k,这里的

c++11-bind的用法

bind函数 在c++11之前,要绑定某个函数.函数对象或者成员函数的不同参数值需要用到不同的转换器,如bind1st.bind2nd.fun_ptr.mem_fun和mem_fun_ref等.在c++11中,绑定参数的方法得以简化.c++11提供了"一站式"绑定模板bind,其用法为: #include <functional> std::bind(待绑定的函数对象/函数指针/成员函数指针,参数绑定值1,参数绑定值2,...,参数绑定值n); bind的第一个参数是待绑定

【转载】C++ function、bind和lambda表达式

本篇随笔为转载,原贴地址:C++ function.bind和lambda表达式. 本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lambda表达式, function对象和bind机制.之所以把这三块放在一起讲,是因为这三块之间有着非常密切的关系,通过对比学习,加深对这部分内容的理解.在开始之间,首先要讲一个概念,closure(闭包),这个概念是理解lambda的基础.下面我们来看看wikipedia上对于计算机领域的closure的定义: A closure (also le

Effective C++ —— 杂项讨论(九)

条款53 : 不要轻忽编译器的警告 请记住: 1. 严肃对待编译器发出的警告信息.努力在你的编译器的最高(最严苛)警告级别下争取“无任何警告”的荣誉. 2. 不要过度倚赖编译器的报警能力,因为不同的编译器对待事情的态度并不相同.一旦移植到另一个编译器上,你原本倚赖的警告信息有可能消失.  条款54 : 让自己熟悉包括TR1在内的标准程序库 TR1代表“Technical Report 1”,TR1宣示了一个新版C++的来临,我们可能称之为Standard C++1.1.不熟悉TR1机能而却奢望成

STL笔记(5)条款49:学习破解有关STL的编译器诊断信息

STL笔记(5)条款49:学习破解有关STL的编译器诊断信息 条款49:学习破解有关STL的编译器诊断信息 用一个特定的大小定义一个vector是完全合法的, vector<int> v(10);    // 建立一个大小为10的vector 而string在很多方面像vector,所以你可能希望可以这么做: string s(10);        // 常识建立一个大小为10的string 这不能编译.string没有带有一个int实参的构造函数.我的一个STL平台像这样告诉我那一点: e