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[] = {,,,,,,,,};
std::vector<int> vec;
std::copy_if(std::begin(arr),std::end(arr),std::back_inserter(vec),std::bind1st(std::less<int>(),)); //将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 绑定为第二个参数,即 value < 6
std::for_each(vec.begin(),vec.end(),disp); //1 2 3 4 5 //使用自定义的仿函数
std::vector<person> vecP;
person p1 = {,"jack"}; vecP.push_back(p1);
person p2 = {,"rose"}; vecP.push_back(p2);
person p3 = {,"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的用法的更多相关文章

  1. not1,not2,bind1st和bind2nd详解

    1.引言 bind1st和bind2nd函数用于将一个二元函数对象(binary functor,bf)转换成一元函数对象(unary functor,uf).为了达到这个目的,它们需要两个参数:要转 ...

  2. c++的bind1st()与bind2nd() 二元算子转一元算子

    bind1st()和bind2nd()是两个函数,用于将二元算子转成一元算子. 何谓二元算子? 比如< > =等等这些就是二元算子,即需要两个操作数的运算符. 何谓一元算子? 比如++ - ...

  3. not1,not2,bind1st,bind2nd

    例子需要包含头文件 #include <vector> #include <algorithm> #include <functional> bind1st和bin ...

  4. 【转】 bind1st bind2nd的使用

    以前在使用stl的过程中发现bind1st和bind2nd这两个函数,当时不太理解什么意思,今天在网上查了一下相关资料发现竟然很简单,下面我就具体解释一下他们的用法. bind1st和bind2nd函 ...

  5. 关于标准库中的ptr_fun/binary_function/bind1st/bind2nd

    http://www.cnblogs.com/shootingstars/archive/2008/11/14/860042.html 以前使用bind1st以及bind2nd很少,后来发现这两个函数 ...

  6. bind1st bind2nd的使用

    STL中的函数 bind1st. bind2nd用于将一个二元算子转换成一元算子,需要两个 参数,要转换的bf和一个值v. 参考:http://blog.csdn.net/simahao/articl ...

  7. c++11-bind的用法

    bind函数 在c++11之前,要绑定某个函数.函数对象或者成员函数的不同参数值需要用到不同的转换器,如bind1st.bind2nd.fun_ptr.mem_fun和mem_fun_ref等.在c+ ...

  8. STL用法大全

    1.    概述 泛型编程思想最早缘于A.Stepanov提出的部分算法可独立于数据结构的论断.20世纪90年代初A.Stepanov和Meng Lee根据泛型编程的理论用C++共同编写了STL.但直 ...

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

    本篇随笔为转载,原贴地址:C++ function.bind和lambda表达式. 本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lambda表达式, function对象和bind机制 ...

随机推荐

  1. java实验一 20135104刘帅

    实验报告 一.实验目的与要求: 实验目的: 1. 使用JDK编译.运行简单的Java程序 2.使用Eclipse 编辑.编译.运行.调试Java程序 实验要求: 1.没有Linux基础的同学建议先学习 ...

  2. Yaf零基础学习总结3-Hello Yaf

    Yaf零基础学习总结3-Hello Yaf 上一次我们已经学习了如何安装yaf了,准备工作做好了之后我们来开始实际的编码了,码农都知道一个经典的语句就是“Hello World”了,今天我们开始入手Y ...

  3. SQLSERVER 2012之AlwaysOn -- 一次硬件升级引发的问题

    这是上周遇到的一个案例:对已有的硬件进行升级而引发的问题,期间还触发了一个比较严重的BUG,可谓多灾多难:不过值得庆幸的是,在一连串连锁问题出现的时候,并没有出现人工操作失误(这往往是在处理故障中风险 ...

  4. 微信JSSDK多图片上传并且解决IOS系统上传一直加载的问题

    微信多图片上传必须挨个上传,也就是不能并行,得串行: 那么我们可以定义一个如下所示的上传函数: var serverIds = []; function uploadImages(localImage ...

  5. 用c#开发微信 (6) 微渠道 - 推广渠道管理系统 1 基础架构搭建

    我们可以使用微信的“生成带参数二维码接口”和 “用户管理接口”,来实现生成能标识不同推广渠道的二维码,记录分配给不同推广渠道二维码被扫描的信息.这样就可以统计和分析不同推广渠道的推广效果. 本系统使用 ...

  6. 配置Linux自动挂载

    使用mount命令来挂载硬件,在Linux重启后这些挂载信息会丢失,因此对应磁盘这类硬件,需要配置自动挂载来保证系统重启时进行自动挂载. 自动挂载信息保存在文件/etc/fstab文件中 查看该文件的 ...

  7. iOS开发——高级技术&内购服务

    内购服务 大家都知道做iOS开发本身的收入有三种来源:出售应用.内购和广告.国内用户通常很少直接 购买应用,因此对于开发者而言(特别是个人开发者),内购和广告收入就成了主要的收入来源.内购营销模式,通 ...

  8. javaweb学习总结(二十五)——jsp简单标签开发(一)

    一.简单标签(SimpleTag) 由于传统标签使用三个标签接口来完成不同的功能,显得过于繁琐,不利于标签技术的推广, SUN公司为降低标签技术的学习难度,在JSP 2.0中定义了一个更为简单.便于编 ...

  9. Xcode 重新下载项目配置文件

    配置文件保存在: ~/Library/MobileDevice/Provisioning Profiles 可以按修改日期排序移走没用的配置文件或者干脆将目录重命名,备份好旧的配置文件后,到Xcode ...

  10. 大家一起写mvc(二)

    上一篇已经看了,我想大家都明白了mvc的原理,今天我们来说一下要写自己mvc框架必须要会的技术. mvc的目录是这样的 src目录是我们核心的mvc代码.这个代码明天讲,今天主要讲的代码都在test目 ...