当前位置: 首页 > article >正文

C++系列-STL容器中统计算法count, count_if

STL容器中统计算法count, count_if

  • 💢功能
  • 💢非自定义类型举例
  • 💢自定义类型举例

💢功能

  • 🥝count用于统计容器中某个特定元素的个数,使用的是==运算符。
  • 🥝count_if根据指定的条件统计满足条件的元素的个数。
  • 🥝如果统计自定义类型,需要重载==运算符或者自定义pred。

💢非自定义类型举例

👉 👉 👉
count(vec1.begin(), vec1.end(), 5),统计vec1中5的数量。
count_if(vec1.begin(), vec1.end(), [](int val) -> bool {return val > 3;}),统计vec1中满足大于3的元素的数量。

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

template<typename T>
void print_vector(const vector<T>& vec)
{
	for (auto i_vec : vec)
	{
		cout << i_vec << " ";
	}
	cout << endl;
}

void test01()
{
	vector<int> vec1 {1, 2, 5, 7, 25, 5, 5, 7, 33, 44};
	print_vector(vec1);

	int cnt = count(vec1.begin(), vec1.end(), 5);
	cout << "count(vec1.begin(), vec1.end(), 5): " << cnt << endl;
}

void test02()
{
	vector<int> vec1 {1, 2, 5, 7, 25, 5, 5, 7, 33, 44};
	print_vector(vec1);
	
	// count_if举例,记录vector中大于3的元素个数
	int cnt = count_if(vec1.begin(), vec1.end(), [](int val) -> bool {return val > 3;});
	cout << "(vec1.begin(), vec1.end(), [](int val) -> bool {val > 10;}): " << cnt << endl;
}

void main()
{
	test01();
	test02();
	system("pause");
}

result:
1 2 5 7 25 5 5 7 33 44
count(vec1.begin(), vec1.end(), 5): 3
1 2 5 7 25 5 5 7 33 44
(vec1.begin(), vec1.end(), [](int val) -> bool {val > 10;}): 8

💢自定义类型举例

👉 👉 👉
自定义类型在使用时,要重载==运算符。
另外,pred也需要重新实现。

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

class Person
{
public:
	Person(string name, int age) : m_name(name), m_age(age) {}
	bool operator==(const Person& per)
	{
		if ((per.m_name == this->m_name) && (per.m_age == this->m_age))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
public:
	string m_name;
	int m_age;
};

class Greater10
{
public:
	bool operator()(Person & per)
	{
		return per.m_age > 10;
	}
};
void print_vector(const vector<Person> &vec)
{
	for (auto i_vec : vec)
	{
		cout << "name: " << i_vec.m_name << ", age: " << i_vec.m_age  << endl;
	}
	cout << endl;
}

void test01()
{
	Person p1("张三", 10);
	Person p2("李四", 11);
	Person p3("王五", 12);
	Person p4("赵六", 10);
	Person p5("王五", 12);
	vector<Person> vec1;
	vec1.push_back(p1);
	vec1.push_back(p2);
	vec1.push_back(p3);
	vec1.push_back(p4);
	vec1.push_back(p5);
	print_vector(vec1);
	int cnt = count(vec1.begin(), vec1.end(), p5);
	cout << "王五的个数: " << cnt << endl;
	cnt = count_if(vec1.begin(), vec1.end(), Greater10());
	cout << "年龄大于10的个数: " << cnt << endl;
}

void main()
{
	test01();
	system("pause");
}

result:
name: 张三, age: 10
name: 李四, age: 11
name: 王五, age: 12
name: 赵六, age: 10
name: 王五, age: 12

王五的个数: 2
年龄大于10的个数: 3

http://www.kler.cn/news/319185.html

相关文章:

  • uniapp调用安卓service实现后台运行
  • 华为OD机试真题-最少交换次数-2024年OD统一考试(E卷)
  • fastadmin前端切换成英文,后台中文,修改JS文件
  • Milvus - 从数据库到 Partition Key 实现多租户
  • STM32 使用 CubeMX 实现按键外部中断
  • flink 为啥使用MemorySegment 来管理内存
  • 性能测试1初步使用Jmeter
  • el-table中根据状态改单元格样式
  • 医学数据分析实训 项目五 分类分析--乳腺癌数据分析与诊断
  • mybatis-plus公共字段自动填充fillStrategy()方法和strictFill()方法
  • Windows环境运行.sh脚本提示找不到wget指令的问题
  • CSS基本概念以及CSS的多种引入方式
  • Python模拟真人鼠标轨迹算法
  • 使用umy-ui 优化带有大量输入框、下拉框的ElementUI el-table
  • Leetcode 1472. 设计浏览器历史记录
  • 开源音频处理项目推荐【持续更新】
  • 《C++设计新思维-泛型编程与设计模式之应用》阅读记录
  • DY按图搜索商品API:解锁电商新趋势
  • LeetCode 257. 二叉树的所有路径,dfs
  • 29. RabbitMQ队列模型
  • 多用户自定义商城小程序源码系统 独立部署 到源代码包以及搭建部署教程
  • 根据源码解析Vue2中对于对象的变化侦测
  • 搭建HAproxy----7层负载均衡集群
  • FDA辅料数据库在线免费查询-药用辅料
  • 灵当CRM multipleUpload.php 文件上传致RCE漏洞复现
  • 双11好物推荐有哪些?五大双十一好货推荐!
  • PHP如何从字符串中删除转义字符
  • 抽奖拼团卷轴模式系统开发小程序源代码解析
  • Flask 第十二课 -- 错误处理
  • 下水道内缺陷识别检测数据集 yolo数据集 共2300张