forked from Light-City/CPlusPlusThings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmap.cpp
More file actions
39 lines (30 loc) · 710 Bytes
/
fmap.cpp
File metadata and controls
39 lines (30 loc) · 710 Bytes
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
//
// Created by light on 20-1-5.
//
#include <iostream>
#include <vector>
using namespace std;
template<
template<typename, typename>
class OutContainer = vector,
typename F, class R>
auto fmap(F &&f, R &&inputs) {
typedef decay_t<decltype(f(*inputs.begin()))> result_type;
OutContainer<result_type, allocator<result_type >> result;
for (auto &&item:inputs) {
result.push_back(f(item));
}
return result;
}
// 对每一个数进行加1操作
int add_1(int x) {
return x + 1;
}
int main() {
vector<int> v{1, 2, 3, 4, 5};
auto result = fmap(add_1, v);
for (auto item:result)
cout << item << " ";
cout<<endl;
return 0;
}