include iostream include vector include algorithm using namespace std

 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <class T, class F>
T mymap(const T& from, const F& func)
{
T to;
for (auto it = from.begin();it!=from.end();++it) to.push_back(func(*it));
return to;
}
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
auto s = 0;
for_each(v.begin(),v.end(),[&s](int n){s+=n;});
std::cout<<s<<"\n";
vector<int> newv = mymap(v,[](int n){return n*3;});
for_each(newv.begin(),newv.end(),[](int n){std::cout<<n<<",";});
//[]{std::cout<<"Hello!";}()
}