C++ bind2nd用法
bind1st和bind2nd是将二元函数转换为一元函数,比如一个比较大小的函数是二元函数,当在某些情况下我们想要固定第一个参数或者第二个参数时吗,就成了一元函数,先看用法
// Created by 开机烫手 on 2018/7/24.#include <iostream>
#include <algorithm>
#include <vector>using namespace std;int main() {vector<int> v;v.push_back(2);v.push_back(1);v.push_back(3);v.push_back(9);v.push_back(7);for (int i = 0; i < v.size(); i++)cout << v[i] << " ";cout << endl;cout << count_if(v.begin(), v.end(), bind2nd(less<int>(), 5) ) << endl;return 0;
}
上面这段代码统计了vector中元素值小于5的元素个数,使用bind2nd将less仿函数的第二个参数绑定为5。接下来看看整个代码的调用过程:
template<typename _Operation, typename _Tp>inline binder2nd<_Operation>bind2nd(const _Operation& __fn, const _Tp& __x){typedef typename _Operation::second_argument_type _Arg2_type;return binder2nd<_Operation>(__fn, _Arg2_type(__x));}
bind2nd是有两个模板参数的函数,从例子中调用的地方可以看到,第一个参数是less<int>的临时匿名函数对象,第二个是int型的数字40,less的代码为
template<typename _Tp>struct less : public binary_function<_Tp, _Tp, bool>{_GLIBCXX14_CONSTEXPRbooloperator()(const _Tp& __x, const _Tp& __y) const{ return __x < __y; }};template<typename _Arg1, typename _Arg2, typename _Result>struct binary_function{/// @c first_argument_type is the type of the first argumenttypedef _Arg1 first_argument_type; /// @c second_argument_type is the type of the second argumenttypedef _Arg2 second_argument_type;/// @c result_type is the return typetypedef _Result result_type;};
可以看到less中重载了()运算符实现比较数字大小的功能,比如使用 less<int>(2,3) 即可调用。在bind2nd中通过 _Operation 记录了less的类型,之后调用了binder2nd
template<typename _Operation>class binder2nd: public unary_function<typename _Operation::first_argument_type,typename _Operation::result_type>{protected:_Operation op;typename _Operation::second_argument_type value;public:binder2nd(const _Operation& __x,const typename _Operation::second_argument_type& __y): op(__x), value(__y) { }typename _Operation::result_typeoperator()(const typename _Operation::first_argument_type& __x) const{ return op(__x, value); }// _GLIBCXX_RESOLVE_LIB_DEFECTS// 109. Missing binders for non-const sequence elementstypename _Operation::result_typeoperator()(typename _Operation::first_argument_type& __x) const{ return op(__x, value); }} _GLIBCXX_DEPRECATED;
这个类中定义了 _Operation 型的op对象,在构造函数中使用之前传入的临时函数对象对其进行了初始化,也重载了 () 运算符,在重载运算符函数中调用了之前的仿函数,这里对应为less(__x,40)。
应该就是这么个过程了。。。
C++ bind2nd用法相关推荐
- C++11 bind取代bind1st,bind2nd的用法
bind2nd的用法: int a[] = {1, 2, 3, 4}; std::vector< int> arr(a, a + 4); // 移除所有小于3的元素 arr.erase( ...
- bind1st bind2nd的使用
bind1st bind2nd的使用 Posted on 2010-07-16 09:14 wanpengcoder 阅读(1882) 评论(0) 编辑 收藏 bind1st和bind2nd函数用于将 ...
- C++for_each| bind1st | ptr_fun | std::function的用法
c++ for_each 用法_小键233-CSDN博客 传入参数 要传入参数给global function ,需要使用 ptr_fun() 这个 function adapter 将global ...
- lambda函数用法总结
文章目录 概念 基础用法 STL与lambda 概念 lambda函数是C++ 11引入的新特性,在其它语言中也有lambda,典型的如javascript.它是函数编程的基础.在C++中,lambd ...
- 【C++】find()函数用法查找
1. 查找指向指定元素的迭代器 find函数主要实现的是在容器内查找指定的元素,并且这个元素必须是基本数据类型的. 查找成功返回一个指向指定元素的迭代器,查找失败返回end迭代器. 2. 在数组中查找 ...
- c++ for_each 用法
主要参考:for_each的用法,转载自蕭鴻森的blog (找不到原文的链接) 在接触C++特性Lambda时,接触到for_each,没想到这内容还挺多的,所以,先了解for_each,再学习La ...
- c++ for_each 的各种用法
引入 先看一段不用for_each 的代码: #include<iostream> #include<vector> #include<algorithm> usi ...
- c语言中external,static关键字用法
static用法: 在C中,static主要定义全局静态变量.定义局部静态变量.定义静态函数. 1.定义全局静态变量:在全局变量前面加上关键字static,该全局变量变成了全局静态变量.全局静态变量有 ...
- Pandas_transform的用法
先来看一个实例问题. 如下销售数据中展现了三笔订单,每笔订单买了多种商品,求每种商品销售额占该笔订单总金额的比例.例如第一条数据的最终结果为:235.83 / (235.83+232.32+107.9 ...
最新文章
- 删除数组中指定元素_如何删除PHP数组元素键值并重新排序
- nginx php页面打开404,nginx php页面 error_page 404不起作用解决
- Mybatis 总结
- 140行代码实现一个逼真的大雪纷飞的效果
- readonly于disable的区别 及 name和id的区别
- 策划入门(一)什么样的创意是可行的
- c语言10怎么打开文件,Lecture 10 C语言文件操作
- linux以命令行下配置连接wlan无线网卡
- 单点登录系统设计分析
- 3d胆码计算机方法,3D百十个位定胆技巧准确率95
- 1980年红色1元纸币值多少钱?
- 数模论文排版—从第三页设置页码,页码格式为page X of Y
- Unix时间戳一天是多少?
- 全部口罩机3D图纸图档打包
- BOJ 1433 Tom 的潜望镜 优先队列+BFS
- 主板电容损坏导致台式机开机风扇转无显示信号输出
- etsy开店流程遇到的问题整理
- 如何将C盘下的“用户”文件夹改为英文“Users”
- 全国计算机二级office选择题知识点,全国计算机二级考试MS office选择题知识点合集(通用)...
- 那些年啊 那些事——一个程序员的奋斗史 ——105