STL binder1st binder2nd bind1st bind2nd区别

binder**和bind**功能对应。只是binder**是类绑定器,bind**为全局函数绑定器而已。

1st和2nd很好理解。一个是第一个参数不变,一个是第二个参数不变。

参看程序:

#include "stdafx.h"

#include <iostream>
#include <algorithm>	// count_if
#include <functional>	// binder
#include <list>

using namespace std;

int main()
{
	// Data
	int iarray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	list<int> aList(iarray, iarray + 10);

	// binder和bind区别:
	// 1. 类绑定器有binder1st和binder2nd,而函数绑定器是bind1st和bind2nd
	// 2. bind是一个全局的模板函数其返回值为一个binder模板类的实例
	// 3. binder要指定泛型
	int k = count_if(aList.begin(), aList.end(), binder1st<greater<int>>(greater<int>(), 5));

	// bind1st bind2nd功能比较
	// k = count_if(aList.begin(), aList.end(), bind1st(greater<int>(), 5)); 
	// bind1st(greater<int>(), 5); //---->5 > x 即5作为第一个固定参数。返回5大于的数字的个数
	// bind2nd(greater<int>(), 5); //---->x > 5 即5作为第二个固定参。返回大于5的数字的个数

	cout << k << endl;

	system("pause");
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章