//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;
}
STL示例09(绑定器bind1st)
最新推荐文章于 2024-02-06 23:43:13 发布