/*count: 函数原型: template <class InputIterator,class Type> typename iterator_traits<InputIterator>::difference_type count(InputIterator first,InpurIterator last,const Type& value); 功能: 在序列[first,last)中查找和value相等的元素个数,统计其个数,返回其值。 count_if: 函数原型: template <class InputIterator,class Predicate> typename iterator_traits<InputIterator>::difference_type count_if(InputIterator first,InputIterator last,Predicate pred); 功能: 在序列[first,last)中统计满足关系pred的元素个数,并返回其值。 */ #include <iostream>#include <algorithm>#include <vector> using namespace std; class Odd{ public: bool operator()(int val) { return val%2 ? true : false; }}; int main(){ int ia[] = {2,3,6,9,8,6,2,6}; vector<int> v(ia,ia+8); assert( count(v.begin(),v.end(),6) == 3); assert( count_if(v.begin(),v.end(),Odd()) == 2); cout << "OK! Test succeed! " << endl; system("pause"); return 0;}

评论