C++
C++
code posted
created at 27 Apr 23:10, updated at 10 May 23:14
Edit
|
Back
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 |
// compile with: --std=c++0x #include <algorithm> #include <iostream> #include <vector> #include <functional> using namespace std; typedef function<int(int)> FuncIntToInt; int main() { vector<int> v; for (int i = 0; i < 5; ++i) { v.push_back(i); } auto addToN = [=](int n)->FuncIntToInt { return [=](int c) { return c + n; }; }; auto addTo5 = addToN(5); auto addTo100 = addToN(100); int count = 0; for_each(v.begin(), v.end(), [&] (int n) { cout << "n = " << n << ":" << endl << "\t addTo5(" << n << ") = " << addTo5(n) << endl << "\t addTo100(" << n << ") = " << addTo100(n) << endl; count++; }); cout << "Executed " << count << " iterations" << endl; return 0; } |
773 Bytes in 3 ms with coderay