#include <iostream>
using namespace std;
#include "set"
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
class IsGreat
{
public:
IsGreat(int i)
{
m_num = i;
}
bool operator()(int &num)
{
if (num > m_num)
{
return true;
}
return false;
}
private:
int m_num;
};
void main22()
{
vector<int> v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i+1);
}
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
int num1 = count(v1.begin(), v1.end(),3);
cout << "num1:" << num1 << endl;
//通过 谓词 求 大于2的个数
int num2 = count_if(v1.begin(),v1.end(),IsGreat(2));
cout << "num2:" << num2 << endl;
//通过预定义的函数对象 求大于2的个数
/*
template<class _Ty = void>
struct greater
: public binary_function<_Ty, _Ty, bool>
{ // functor for operator>
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator> to operands
return (_Left > _Right);
}
};
*/
//greater<int>() 有两个参数 左参数来自容器的元素,右参数固定成2 (通过bind2nd做的)
int num3 = count_if(v1.begin(),v1.end(),bind2nd(greater<int>(),2));
cout << "num3:" << num3 << endl;
// 求 奇数的个数
int num4 = count_if(v1.begin(), v1.end(), bind2nd(modulus<int>(), 2));
cout << "奇数的个数num4:" << num4 << endl;
//求 偶数的个数
int num5= count_if(v1.begin(), v1.end(),not1( bind2nd(modulus<int>(), 2)));
cout << "偶数的个数num5:" << num5 << endl;
}
void main()
{
main22(); //函数适配器综合案例
}