// CountIf.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
//函数模板
template <typename T>
bool is_odd(const T& t)
{
return (t%2) ? true : false;
}
//结构体模板(谓词)
template<typename T>
struct is_even : public unary_function<T,bool>
{
bool operator()(const T& t)
{
return (t%2) ? false : true;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
typedef vector<int>::iterator ITERATOR;
typedef vector<int>::size_type SIZETYPE;
vector<int> intVec;
SIZETYPE sz;
for(sz = 0; sz < 10; sz++)
{
intVec.push_back(sz + rand()%100);
}
size_t size = count_if(intVec.begin(),intVec.end(),is_odd<int>); //谓词部分无括号,传的是实例化的函数指针
size = count_if(intVec.begin(),intVec.end(),is_even<int>()); //谓词部分有括号,传的是模板实例化对象
return 0;
}