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. Create My MySQL configuration by Percona

    本文地址:http://www.cnblogs.com/yhLinux/p/4013065.html https://tools.percona.com/ Percona是一款在线自动生成MySQL配 ...

  2. 【4_237】Delete Node in a Linked List

    Delete Node in a Linked List Total Accepted: 48121 Total Submissions: 109297 Difficulty: Easy Write ...

  3. 使用的组件:ckeditor

    老牌Web文本编辑器,无需多言. 官网地址:http://ckeditor.com/

  4. Xshell_Using X11 forwarding

    FROM:http://www.netsarang.com/tutorial/xshell/1018/Using_X11_forwarding The X11 forwarding feature i ...

  5. Delphi 的字符及字符串[5] - 字符串与 Windows API

    先说赋值: //赋值方法1: 给直接量 begin   SetWindowText(Handle, '新标题'); end; //赋值方法2: 定义它要的类型 var   p: PChar; begi ...

  6. sublime Text3 插件编写教程_第一课

    今天给大家分享一下编写一个Sublime Text3 插件的流程以及使用插件解决的一个实际问题. 一.开发插件的前提条件 开发sublime插件用到的是Python语言,因此必须懂Python语言的基 ...

  7. 用msbuild跑xunit单元测试

    用了Visual Studio 2015之后,发现没法跑xUnit单元测试,xUnit.net runner不支持VS2015,TestDriven.Net也不支持VS2015. 等它们支持VS201 ...

  8. [MSSQL] Useful SQL Scripts - CalendarBase

    Useful SQL scripts DECLARE @StartDate DATETIME DECLARE @EndDate DATETIME DECLARE @FiscalBeginMonth I ...

  9. DFD数据流程图

    顶层图DFD 0层图 1层图

  10. Nginx学习笔记(三) Nginx基本数据结构

    Nginx基本数据结构 话说学习一种编程语言,例如C语言,我们首先学的也是数据结构,这是以后开发程序的关键.为了更好更方便的开发Nginx,Nginx自己实现了很多适合nginx的数据结构. Ngin ...