#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;
}
83 常用查找算法-count
最新推荐文章于 2024-10-16 19:08:56 发布
本文介绍了C++中STL(StandardTemplateLibrary)中的算法,如count函数,通过test01和test02函数展示了如何使用count在整数向量和Person对象向量中查找特定元素的个数。
摘要由CSDN通过智能技术生成