#include <iostream>
#include <string>
#include <functional>
#include <vector>
#include <algorithm>
using namespace std;
//stl常见算法
/*
1.algorithm
2.numeric
3.functional
*/
class Person
{
public:
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
class great20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
class less20
{
public:
bool operator()(const Person &p)const
{
return p.m_age < 20;
}
};
//常用查找算法count_if
//内置数据类型
void test01()
{
vector<int>v1;
v1.push_back(11);
v1.push_back(33);
v1.push_back(2);
v1.push_back(46);
v1.push_back(78);
v1.push_back(112);
int num = count_if(v1.begin(), v1. end(), great20());
cout << "大于20的个数:" << num << endl;
}
void test02()
{
vector<Person>v2;
Person p1("刘备", 12);
Person p2("张飞", 15);
Person p3("关羽", 23);
Person p4("赵云", 22);
Person p5("曹操", 28);
v2.push_back(p1);
v2.push_back(p2);
v2.push_back(p3);
v2.push_back(p4);
v2.push_back(p5);
int num = count_if(v2.begin(), v2.end(), less20());
cout << "小于20的个数:" << num << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
84 常用查找算法-count_if
最新推荐文章于 2024-10-01 23:05:47 发布