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();
}

猜你喜欢

转载自blog.csdn.net/qq_33762043/article/details/79936520
1st