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

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

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

   bool operator==(const Person &p)
   {
      return p.age == age;
   }

   string name;
   int age;
};

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

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

   Person p4("lisi", 10);

   int num = count(v.begin(), v.end(), p4);
   cout << "和李四同岁的人数有 " << num << endl;

   return 0;
}
$ ./a.out      
和李四同岁的人数有 2
posted @ 2022-07-31 13:27  thomas_blog  阅读(41)  评论(0编辑  收藏  举报