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的元素的数量。

cpp 复制代码
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也需要重新实现。

cpp 复制代码
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
相关推荐
互联网搬砖老肖4 分钟前
python成功解决AttributeError: can‘t set attribute ‘lines‘
开发语言·python
张立龙66622 分钟前
有序二叉树各种操作实现(数据结构C语言多文件编写)
c语言·开发语言·数据结构
2401_8454174523 分钟前
C++ string类
java·开发语言·c++
23zhgjx-ctl27 分钟前
Isis-路由引入
开发语言·php
一瞬祈望32 分钟前
从零开始:Python运行环境之VSCode与Anaconda安装配置全攻略 (1)
开发语言·vscode·python
Y.O.U..37 分钟前
力扣HOT100——560.和为k的子数组
数据结构·c++·算法·leetcode
wuqingshun31415942 分钟前
经典算法 判断一个图中是否有环
java·开发语言·数据结构·c++·算法·蓝桥杯·深度优先
柃歌42 分钟前
【LeetCode Solutions】LeetCode 160 ~ 165 题解
数据结构·算法·leetcode
小森77671 小时前
(五)机器学习---决策树和随机森林
算法·决策树·随机森林·机器学习·分类算法
神仙别闹1 小时前
基于JSP+MySQL实现用户注册登录及短信发送功能
java·开发语言·mysql