STL count_if应用

来源:互联网 发布:数据库攻击手段sql注入 编辑:程序博客网 时间:2024/05/03 11:23
// 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;}

0 0
原创粉丝点击