84 常用查找算法-count_if

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值