-
Notifications
You must be signed in to change notification settings - Fork 2
/
T75.cpp
34 lines (27 loc) · 1.04 KB
/
T75.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
//TODO count count_if 算法包里的 统计元素的个数
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main75() {
vector<int> vectorVar;
vectorVar.push_back(11);
vectorVar.push_back(22);
vectorVar.push_back(11);
vectorVar.push_back(33);
vectorVar.push_back(22);
vectorVar.push_back(11);
vectorVar.push_back(11);
vectorVar.push_back(22);
// count使用 第一种方法
int number = count(vectorVar.begin(), vectorVar.end(), 22);
cout << "等于22的个数是:" << number << endl;
//c++源码 函数适配器 第二种方法
number = count_if(vectorVar.begin(), vectorVar.end(), bind2nd(less<int>(), 22));
cout << "小于22的个数是:" << number << endl;
number = count_if(vectorVar.begin(), vectorVar.end(), bind2nd(greater<int>(), 22));
cout << "大于22的个数是:" << number << endl;
number = count_if(vectorVar.begin(), vectorVar.end(), bind2nd(equal_to<int>(), 22));
cout << "等于22的个数是:" << number << endl;
return 0;
}