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[] = {,,,,,,,,};
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的用法的更多相关文章
- not1,not2,bind1st和bind2nd详解
1.引言 bind1st和bind2nd函数用于将一个二元函数对象(binary functor,bf)转换成一元函数对象(unary functor,uf).为了达到这个目的,它们需要两个参数:要转 ...
- c++的bind1st()与bind2nd() 二元算子转一元算子
bind1st()和bind2nd()是两个函数,用于将二元算子转成一元算子. 何谓二元算子? 比如< > =等等这些就是二元算子,即需要两个操作数的运算符. 何谓一元算子? 比如++ - ...
- not1,not2,bind1st,bind2nd
例子需要包含头文件 #include <vector> #include <algorithm> #include <functional> bind1st和bin ...
- 【转】 bind1st bind2nd的使用
以前在使用stl的过程中发现bind1st和bind2nd这两个函数,当时不太理解什么意思,今天在网上查了一下相关资料发现竟然很简单,下面我就具体解释一下他们的用法. bind1st和bind2nd函 ...
- 关于标准库中的ptr_fun/binary_function/bind1st/bind2nd
http://www.cnblogs.com/shootingstars/archive/2008/11/14/860042.html 以前使用bind1st以及bind2nd很少,后来发现这两个函数 ...
- bind1st bind2nd的使用
STL中的函数 bind1st. bind2nd用于将一个二元算子转换成一元算子,需要两个 参数,要转换的bf和一个值v. 参考:http://blog.csdn.net/simahao/articl ...
- c++11-bind的用法
bind函数 在c++11之前,要绑定某个函数.函数对象或者成员函数的不同参数值需要用到不同的转换器,如bind1st.bind2nd.fun_ptr.mem_fun和mem_fun_ref等.在c+ ...
- STL用法大全
1. 概述 泛型编程思想最早缘于A.Stepanov提出的部分算法可独立于数据结构的论断.20世纪90年代初A.Stepanov和Meng Lee根据泛型编程的理论用C++共同编写了STL.但直 ...
- 【转载】C++ function、bind和lambda表达式
本篇随笔为转载,原贴地址:C++ function.bind和lambda表达式. 本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lambda表达式, function对象和bind机制 ...
随机推荐
- 如何查看MySQL执行计划
在介绍怎么查看MySQL执行计划前,我们先来看个后面会提到的名词解释: 覆盖索引: MySQL可以利用索引返回select列表中的字段,而不必根据索引再次读取数据文件 包含所有满足查询需要的数据的索引 ...
- sql like 查询
查询 ids 含有 4 的 精确到4 54 不查询 select * from t_g_sku where ','||ids||',' like '%,4,%'; 序号 id mid quant ...
- python基础整理笔记(二)
一. 列表 1. 创建实例: a = [1,2,3] b = list() 2. 主要支持的操作及其时间复杂度如下: 3. 其他 python中的列表,在内存中实际存储的形式其实是分散的存储,比较类似 ...
- C++混合编程之idlcpp教程Lua篇(2)
在上一篇 C++混合编程之idlcpp教程(一) 中介绍了 idlcpp 工具的使用.现在对 idlcpp 所带的示例教程进行讲解,这里针对的 Lua 语言的例子.首先看第一个示例程序 LuaTuto ...
- 深入浅出话VC++(2)——MFC的本质
一.引言 上一专题中,纯手动地完成了一个Windows应用程序,然而,在实际开发中,我们大多数都是使用已有的类库来开发Windows应用程序.MFC(Microsoft Foundation Clas ...
- .NET跨平台:在mac命令行下用vim手写ASP.NET 5 MVC程序
昨天在 Mac 上手写了一个最简单的 ASP.NET 5 程序,直接在 Startup.cs 中通过 Response.WriteAsync() 输出响应内容,详见 .NET跨平台:在Mac上跟着错误 ...
- [芯片][MPU6050] MPU60X0的DMP相关链接
标题:发个自己做的UD分解+强跟踪卡尔曼滤波做的双轴姿态测量 链接:http://www.amobbs.com/thread-5511854-1-1.html 关键词:UD分解+强跟踪卡尔曼滤波,采用 ...
- ubuntu14.04中文楷体变默认字体
使用ubuntu以来,最让人头疼的事情就是在英文系统里面使用中文,一般中文字体都很难看,要么有锯齿,要么就是楷体.经过网上搜索找到一堆方法.一个个尝试之后觉得以下方式是最简单有效的. 1.安装font ...
- iOS开发---集成百度地图完善版
一.成为百度的开发者.创建应用 http://developer.baidu.com/map/index.php?title=首页 (鼠标移向 然后选择你的项目需要的功能 你可以在里面了解到你想要使用 ...
- 匿名管道读取CMD回显信息
之前用了很坑爹的做法去读取了cmd命令的回显信息,现在发现了用匿名管道的实现方法,由于楼主没有学过Windows核心编程,找了一个代码来凑数 存下来以后研究 #include <windows. ...