STL bind1st bind2nd详解

转载自:STL bind1st bind2nd详解
2010年9月19日 代码疯子
 
先不要被吓到,其实这两个配接器很简单。
首先,他们都在头文件<functional>中定义。
其次,bind就是绑定的意思,而1st就代表first,2nd就代表second,现在名在可以很快记住了。
再次,他们的申明是一样的,都是(const Operation& op, const T& x)。


简单的说,bind1st(const Operation& op, const T& x)就是这么一个操作:x op value,而bind2nd(const Operation& op, const T& x)就是这么一个操作:value op x,其中value是被应用bind的对象。这两个配接器都用于将一个二元算子转换成一个一元算子。下面来看一段代码吧!

//Coded by www.programlife.net
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;


int main()
{
	vector<int> coll;
	for(int i = 1; i <= 10; ++i)
	{
		coll.push_back(i);
	}
	//查找元素值大于10的元素的个数
	//也就是使得10 < elem成立的元素个数
	int res = count_if(coll.begin(), coll.end(), bind1st(less<int>(), 10));
	cout << res << endl;
	//查找元素值小于10的元素的个数
	//也就是使得elem < 10成立的元素个数
	res = count_if(coll.begin(), coll.end(), bind2nd(less<int>(), 10));
	cout << res << endl;


	return 0;
}

程序的输出结果是0 9

Copyed From 程序人生 
Home Page:http://www.programlife.net 
Source URL:http://www.programlife.net/stl-bind1st-bind2nd-demo.html 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值