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



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