统计算法count_if自定义数据类型

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Person
{
public:
   Person(string name, int age):
   name(name),
   age(age)
   {}

   string name;
   int age;
};

class Count
{
public:
   bool operator()(const Person &p)
   {
      return p.age > 8;
   }
};

int main()
{
   Person p1("furong", 10);
   Person p2("quange", 8);
   Person p3("zhangsan", 20);

   vector<Person> v;
   v.push_back(p1);
   v.push_back(p2);
   v.push_back(p3);

   int num = count_if(v.begin(), v.end(), Count());
   cout << "大于8岁人数 " << num << endl;

   return 0;
}
$ ./a.out         
大于8岁人数 2
posted @ 2022-07-31 15:34  thomas_blog  阅读(25)  评论(0编辑  收藏  举报