-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtransform_python_map.cpp
45 lines (34 loc) · 1.03 KB
/
transform_python_map.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
#include <iostream>
#include <vector>
#include <set>
#include <algorithm> //transform
using namespace std;
//https://stackoverflow.com/questions/908272/stdback-inserter-for-a-stdset
int main() {
vector<int> myvec = {1,2,3};
vector<int> myemptyvec;
set<int> myset = {1,2,3}, tmp;
//target vector's size is the same as source vector's
transform(myvec.begin(), myvec.end(), myvec.begin(), [](int& x){return x+1;});
//target vector is empty, need to use "back_inserter"
transform(myvec.begin(), myvec.end(), back_inserter(myemptyvec), [](int& x){return x+1;});
//cannot add "&" in lambda function!
transform(myset.begin(), myset.end(), inserter(tmp, tmp.end()), [](int x){return x+1;});
swap(myset, tmp);
for(int e : myvec){
cout << e << " ";
}
cout << endl;
for(int e : myemptyvec){
cout << e << " ";
}
cout << endl;
for(int e : myset){
cout << e << " ";
}
cout << endl;
return 0;
}
//2 3 4
//3 4 5
//2 3 4