查找算法count_if
发布日期:2022-03-04 12:48:36 浏览次数:27 分类:技术文章

本文共 978 字,大约阅读时间需要 3 分钟。

黑马250

#include
#include
#include
#include
using namespace std;
//统计内置数据类型
class greater5
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void test01()
{
vectorv;
for (int i = 0; i < 20; i++)
{
v.push_back(i);
}
int num = count_if(v.begin(), v.end(),greater5());
cout << “查找大于5元素的个数” << num;
}
class person
{
public:
person(string name, int age)
{
m_name = name;
m_age = age;
}
bool operator==(const person& p)
{
if (p.m_age == this->m_age)
{
return true;
}
else
{
return false;
}
}
string m_name;
int m_age;
};
class greater20
{
public:
bool operator()(const person &p)
{
return p.m_age > 20;
}
};
//自定义数据类型
void test02()
{
vectorv;
person p1(“aa”, 10);
person p2(“bb”, 20);
person p3(“cc”, 30);
person p4(“dd”, 30);
person p5(“ee”, 20);

person p("pp", 30);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);v.push_back(p);int num = count_if(v.begin(), v.end(),greater20());cout << "岁数大于20 人的个数:" << num << endl;

}

int main()
{
test02();
}

转载地址:https://blog.csdn.net/qq_43752920/article/details/120548915 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:查找算法count
下一篇:常用排序算法sort

发表评论

最新留言

很好
[***.229.124.182]2024年04月14日 07时36分30秒