-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path07_查找_count.cpp
86 lines (72 loc) · 1.38 KB
/
07_查找_count.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// count
/*
count(iterator beg, iterator end, value);
统计元素出现次数
beg 开始迭代器
end 结束迭代器
value 统计的元素
*/
// 统计内置数据类型
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(40);
v.push_back(30);
v.push_back(40);
v.push_back(10);
v.push_back(40);
int num = count(v.begin(), v.end(), 40);
cout << "40 的元素个数为: " << num << endl;
}
// 统计自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
// 重载 == 底层 find 知道如何对比 Person 数据类型
bool operator==(const Person &p) const
{
if (this->m_Age == p.m_Age)
{
return true;
}
else
{
return false;
}
}
string m_Name;
int m_Age;
};
void test02()
{
vector<Person> v;
Person p1("a", 20);
Person p2("b", 22);
Person p3("c", 22);
Person p4("d", 23);
Person p5("e", 23);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
Person pp("f", 22);
int num = count(v.begin(), v.end(), pp);
cout << num << endl;
}
int main()
{
// test01();
test02();
return 0;
}