Title / Description
Code // 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; }
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code