std::bind1st std::bind2nd boost::bind

2 篇文章 0 订阅
#include <iostream>
#include <string>
#include <functional>
#include <vector>

#include <boost/typeof/typeof.hpp>

#include <boost/assign.hpp>
#include <boost/bind.hpp>


using namespace boost::assign;

void print(int v)
{
	std::cout << v <<" ";
}

void print2(int v,int y)
{
	std::cout << v + y <<" ";
}

// binary_function 的三个参数为 第一个参数类型,第二个参数类型,函数的返回值类型
struct test2:public std::binary_function<int, std::string,void> 
{
	void operator()(int x,std::string y)const
	{
		std::cout << x << y << " ";
	}
};


struct test1:public std::binary_function<std::string,int, void> 
{
	void operator()(std::string y,int x)const
	{
		std::cout << x << y << " ";
	}
};


int main()
{
	std::vector<int> v;

	boost::assign::push_back(v),2,1,3,7,5,6;

	std::for_each(v.begin(),v.end(),print);

	std::cout << std::endl;

	std::for_each(v.begin(),v.end(),std::bind1st(test1(),"test"));

	std::cout << std::endl;

	std::for_each(v.begin(),v.end(),std::bind2nd(test2(),"test"));

	std::cout << std::endl;

	std::cout << std::count_if(v.begin(),v.end(),std::bind1st(std::greater<int>(),10)) << std::endl;


	std::for_each(v.begin(),v.end(),boost::bind(print2,10,_1));

	std::cout << std::endl;

	std::for_each(v.begin(),v.end(),boost::bind(print2,_1,11));

	std::cout << std::endl;


	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值