forked from demon90s/CppStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_14_42.cpp
60 lines (48 loc) · 1.08 KB
/
exercise_14_42.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
// 练习14.42:使用标准库函数对象及适配器定义一条表达式,令其
// (a) 统计大于1024的值有多少个。
// (b) 找到第一个不等于pooh的字符串。
// (c) 将所有的值乘以2。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void func_a()
{
vector<int> vec = {1, 2, 1025, 2000, 1024};
auto adapter = bind(greater<int>(), placeholders::_1, 1024);
auto cnt = count_if(vec.cbegin(), vec.cend(), adapter);
cout << cnt << endl;
}
void func_b()
{
vector<string> vec = {"pooh", "Hi", "A"};
auto adapter = bind(not_equal_to<string>(), placeholders::_1, "pooh");
auto it = find_if(vec.cbegin(), vec.cend(), adapter);
if (it != vec.cend())
{
cout << *it << endl;
}
else
{
cout << "Not found" << endl;
}
}
void func_c()
{
vector<int> vec = {1, 2, 3, 4};
auto adapter = bind(multiplies<int>(), placeholders::_1, 2);
transform(vec.cbegin(), vec.cend(), vec.begin(), adapter);
for (auto i : vec)
{
cout << i << " ";
}
cout << endl;
}
int main()
{
//func_a();
//func_b();
func_c();
return 0;
}