1 | #include <functional> |
---|---|
2 | #include <stdlib.h> |
3 | |
4 | template<typename ElemType> |
5 | ElemType* alloc(size_t count, std::function<ElemType(size_t)> get) |
6 | { |
7 | ElemType *elems = new ElemType[count]; |
8 | for(size_t i = 0; i < count; i++) |
9 | elems[i] = get(i); |
10 | return elems; |
11 | } |
12 | |
13 | int main (int argc, const char * argv[]) |
14 | { |
15 | int* data = alloc<int>(count: 5, get: [] (size_t idx) -> int { |
16 | return 2 * idx + 1; |
17 | }); |
18 | return 0; // break here |
19 | } |
20 | |
21 |