-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
51 lines (41 loc) · 1.16 KB
/
test.cpp
File metadata and controls
51 lines (41 loc) · 1.16 KB
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
#include "lambda-commented.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class Mult10 {
public:
int operator()(int &v);
};
int Mult10::operator()(int &v) {
return v * 10;
}
int main(void) {
/* Initial Vector */
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
/*
* Using the Lambda Library to multiply by 10
*/
printf("=== Lambda Library \\w transform() ===\n");
transform(v.begin(), v.end(), v.begin(), _1 * 10);
for_each(v.begin(), v.end(), cout << _1 << "\n");
/*
* Using the Lambda library to multiply by 10
* with the assignment operator.
*/
printf("=== Lambda Library \\w operator=() ===\n");
for_each(v.begin(), v.end(), _1 = _1 * 10);
for_each(v.begin(), v.end(), cout << _1 << "\n");
printf("=== Mult10 class with Functor ===\n");
Mult10 f;
transform(v.begin(), v.end(), v.begin(), f);
for_each(v.begin(), v.end(), cout << _1 << "\n");
printf("=== std::multiplies ===\n");
transform(v.begin(), v.end(), v.begin(), bind2nd(std::multiplies<int>(), 10));
for_each(v.begin(), v.end(), cout << _1 << "\n");
return 0;
}