83 常用查找算法-count

本文介绍了C++中STL(StandardTemplateLibrary)中的算法,如count函数,通过test01和test02函数展示了如何使用count在整数向量和Person对象向量中查找特定元素的个数。
摘要由CSDN通过智能技术生成
#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;
	}
	bool operator==(const Person &p)
	{
		return this->m_age == p.m_age;
	}
	string m_name;
	int m_age;

};

//常用查找算法count

//内置数据类型
void test01()
{
	vector<int>v1;
	v1.push_back(112);
	v1.push_back(112);
	v1.push_back(1122);
	v1.push_back(1124);
	v1.push_back(112);
	v1.push_back(112);

	int num = count(v1.begin(), v1. end(), 112);
	cout << "112 的个数:" << num << endl;

}

void test02()
{
	vector<Person>v2;

	Person p1("六倍", 22);
	Person p2("san倍", 22);
	Person p3("ds", 23);
	Person p4("六fdf倍", 22);
	Person p5("gfg", 28);
	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);
	v2.push_back(p5);

	Person p6("ffff", 28);

	int num = count(v2.begin(), v2.end(), p6);

	cout << "和 fff  相同年里的个数:" << num << endl;

}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值