bind1st实现

#include <algorithm>
#include <iostream>

using namespace std;
template<typename _Arg1, typename _Arg2, typename _Result>
struct binary_function2 {
	typedef _Arg1 first_argument_type;
	typedef _Arg2 second_argument_type;
	typedef _Result result_type;
};

struct equal_to2 : public binary_function2<int, int, bool> {
	bool operator()(const int& __x, const int& __y) const {
		return __x == __y;
	}
};


template<typename _Operation>
class binder1st2  {
protected:
	_Operation op;
	typename _Operation::first_argument_type value;

public:
	binder1st2(const _Operation& __x,
		const typename _Operation::first_argument_type& __y) :
		op(__x), value(__y) {
		cout << "run1" << endl; 
	}

	typename _Operation::result_type operator()(
		const typename _Operation::second_argument_type& __x) const {
		cout << "run2" << endl;
		return op(value, __x);
	}
};

template<typename _Operation, typename _Tp>
inline binder1st2<_Operation> bind1st2(const _Operation& __fn, const _Tp& __x) {
	typedef typename _Operation::first_argument_type _Arg1_type;
	return binder1st2<_Operation>(__fn, _Arg1_type(__x));
}
void go(int a){

}
int main() {
	int numbers[] = { 10, 20, 30, 40, 50, 10 };
	int cx;
	cx = std::count_if(numbers, numbers + 6, bind1st2(equal_to2(), 10));
	cout << "There are " << cx << " elements that are equal to 10.\n";
	getchar();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值