count和count_if函数(C++)

本文详细介绍了C++标准库中的count和count_if函数,如何用于统计内置和自定义数据类型的元素出现次数,并展示了重载运算符在自定义类型统计中的关键作用。通过实例演示了如何使用这两个函数处理整型和Person类对象,以及按条件统计元素。
摘要由CSDN通过智能技术生成

count

功能描述:

  • 统计元素个数,返回一个整形变量

函数原型:

  • count(iterator beg, iterator end, value);

    // 统计元素出现次数

    // beg 开始迭代器

    // end 结束迭代器

    // value 统计的元素



底层对比方法:
在这里插入图片描述



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




// 遍历函数
void Myprint1(int n) {

	cout << n << "    ";
}

// 查找内置数据类型
void test01() {

	vector<int> v1;
	v1.push_back(10);
	v1.push_back(201);
	v1.push_back(3);
	v1.push_back(6);
	v1.push_back(58);
	v1.push_back(79);
	v1.push_back(6);
	v1.push_back(6);
	v1.push_back(6);
	cout << "针对内置数据类型" << endl;
	cout << "容器v1里的元素如下:" << endl;
	for_each(v1.begin(), v1.end(), Myprint1);
	cout << endl << endl;

	// 统计元素6出现的次数
	int num1 = count(v1.begin(), v1.end(), 6);

	if (num1 == 0) {
		cout << "容器v1里没有6这个元素!" << endl << endl;
	}
	else
	{
		cout << "容器v1里6出现" << num1 <<"次"<< endl << endl;
	}
	cout << endl << endl;


	// 统计元素8出现的次数
		int num2 = count(v1.begin(), v1.end(), 8);

	if (num2 == 0) {
		cout << "容器v1里没有8这个元素!" << endl << endl;
	}
	else
	{
		cout << "容器v1里8出现" << num2 << "次" << endl << endl;
	}
	cout << endl << endl;






}








// 查找自定义数据类型
class Person {
public:

	Person(string name, int age) :m_Age(age), m_Name(name) {}

	int m_Age;
	string m_Name;



	// 重载==
	// 不然怎么比较两个对象是否相等呢
	bool operator==(const Person& p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		return false;
	}



};

void Myprint2(Person p) {
	cout << "姓名:" << p.m_Name << "     "
		<< "年龄:" << p.m_Age << endl;
}


void test02() {
	cout << "针对自定义数据类型" << endl;

	vector<Person> v2;

	//创建数据
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	Person p5("fff", 50);
	Person p6("ggg", 60);

	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);
	v2.push_back(p3);
	v2.push_back(p3);
	v2.push_back(p3);
	v2.push_back(p3);
	v2.push_back(p3);
	v2.push_back(p3);	v2.push_back(p3);	v2.push_back(p3);
	v2.push_back(p3);
	cout << "容器v2里的元素如下:" << endl;
	for_each(v2.begin(), v2.end(), Myprint2); cout << endl << endl;

	int num2 = count(v2.begin(), v2.end(), p3);
	if (num2 == 0) {
		cout << "容器v2里没有p3这个对象!" << endl << endl;
	}
	else
	{
		cout << "容器v2里p3出现" << num2 << "次" << endl;
		cout << "p3的姓名为:" << p3.m_Name << "    "
			<< "p3的年龄为:" << p3.m_Age << endl << endl;
	}
	cout << endl << endl;


	int num3 = count(v2.begin(), v2.end(), p6);
	if (num3 == 0) {
		cout << "容器v2里没有p6这个对象!" << endl << endl;
	}
	else
	{
		cout << "容器v2里p6出现" << num2 << "次" << endl;
		cout << "p6的姓名为:" << p3.m_Name << "    "
			<< "p6的年龄为:" << p3.m_Age << endl << endl;
	}
	cout << endl << endl;


}





int main() {
	test01();
	test02();

	return  0;


}

在这里插入图片描述

总结: 统计自定义数据类型时候,需要配合重载 operator==





2.2.6 count_if

功能描述:

  • 按条件统计元素个数

函数原型:

  • count_if(iterator beg, iterator end, _Pred);

    // 按条件统计元素出现次数

    // beg 开始迭代器

    // end 结束迭代器

    // _Pred 谓词。在什么情况下才统计元素个数



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


void Myprint1(int n) {

	cout << n << "    ";

}
bool Mycompare1(int a, int b) {

	return a > b;
}


void test01()
{
	cout << "针对内置数据类型" << endl;
	vector<int> v1;
	v1.push_back(10);
	v1.push_back(201);
	v1.push_back(3);
	v1.push_back(6);
	v1.push_back(58);
	v1.push_back(79);
	v1.push_back(6);
	v1.push_back(6);
	v1.push_back(6);
	cout << "v1里的元素如下:" << endl;

	for_each(v1.begin(), v1.end(), Myprint1); cout << endl << endl;

	reverse(v1.begin(), v1.end());
	cout << "v1反转后,v1里的元素如下:" << endl;

	for_each(v1.begin(), v1.end(), Myprint1); cout << endl << endl;
	
}



// 针对自定义数据类型
class Person {
public:
	Person(string name, int age) :m_Name(name), m_Age(age) {

	}

	int m_Age;
	string m_Name;



};




class Mycompare2 {
public:
	bool operator()(Person p1, Person p2) {

		return   p1.m_Age < p2.m_Age;

	}


};
class Mycompare3 {
public:
	bool operator()(Person p1,Person p2) {
		return p1.m_Age=p2.m_Age;
	}
};
void Myprint2(Person p) {
	cout << "姓名:" << p.m_Name << "    "
		<< "年龄:" << p.m_Age << endl;
}





void test02() {
	cout << "针对自定义数据类型" << endl;
	vector<Person> v2;

	//创建数据
	Person p1("aaa", 10);
	Person p2("bbb", 5);
	Person p3("ccc", 60);
	Person p4("ddd", 30);
	Person p5("fff", 3);
	Person p6("fff", 200);
	Person p7("fff", 105);


	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);
	v2.push_back(p5);
	v2.push_back(p6);
	v2.push_back(p7);

	cout << "v2里的元素如下:" << endl;
	for_each(v2.begin(), v2.end(), Myprint2); cout << endl << endl;

	reverse(v2.begin(), v2.end());

	cout << "v2反转后,里面的元素如下:" << endl;
	for_each(v2.begin(), v2.end(), Myprint2); cout << endl << endl;

}
int main() {

	test01();

	cout << endl << endl << endl;

	test02();
	cout << endl << endl << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

总结:按值统计用count,按条件统计用count_if
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值