`
sjk2013
  • 浏览: 2190785 次
文章分类
社区版块
存档分类
最新评论

STL示例09(绑定器bind1st)

 
阅读更多
//STL示例 绑定器函数对象bind1st
#include <iostream>
#include <algorithm>
#include <functional>
#include <list>
using namespace std;

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

int main()
{
	int k = 0;
	//count_if统计满足条件的元素个数
	/*其中greater()实现如下
	template<class T>
    struct greater : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const 
	{return (x>y);};
    };
	bind1st函数作用是将8绑定到greater的第一个参数上,看上面greater类型定义及实现
	可以知道这里是求小于8的元素*/
	k=count_if(aList.begin(), aList.end(),bind1st(greater<int>(),8));
	cout << "小于8的数的个数 :" << k << endl;
	return 0;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics