count和count_if函数(C++)

count

功能描述:

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

函数原型:

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

    // 统计元素出现次数

    // beg 开始迭代器

    // end 结束迭代器

    // value 统计的元素



底层对比方法:
count和count_if函数(C++)_第1张图片



#include
using namespace std;
#include
#include
#include
#include




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

	cout << n << "    ";
}

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

	vector 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 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;


}

count和count_if函数(C++)_第2张图片

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





2.2.6 count_if

功能描述:

  • 按条件统计元素个数

函数原型:

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

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

    // beg 开始迭代器

    // end 结束迭代器

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



#include
using namespace std;
#include
#include


void Myprint1(int n) {

	cout << n << "    ";

}
bool Mycompare1(int a, int b) {

	return a > b;
}


void test01()
{
	cout << "针对内置数据类型" << endl;
	vector 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 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函数(C++)_第3张图片

总结:按值统计用count,按条件统计用count_if

你可能感兴趣的:(C++,c++,算法,开发语言)