// 1 #include <algorithm> #include <cstdlib> #include <iostream> #include <vector> using namespace std; int main() { vector<int> srcVec; for (int val = 0; val < 10; val++) { srcVec.push_back(val); } for_each(srcVec.begin(), srcVec.end(), [](int _n) { cout << _n << " "; }); cout << endl; return EXIT_SUCCESS; }
// 2 #include <algorithm> #include <cstdlib> #include <iostream> #include <vector> using namespace std; class MyLambda { public: void operator ()(int _x) const { cout << _x << " "; } }; int main() { vector<int> srcVec; for (int val = 0; val < 10; val++) { srcVec.push_back(val); } for_each(srcVec.begin(), srcVec.end(), MyLambda()); cout << endl; return EXIT_SUCCESS; }
0 1 2 3 4 5 6 7 8 9
// 3 #include <algorithm> #include <cstdlib> #include <iostream> #include <vector> using namespace std; int main() { vector<int> srcVec; for (int val = 0; val < 10; val++) { srcVec.push_back(val); } int result = count_if(srcVec.begin(), srcVec.end(), [] (int _n) { return (_n % 2) == 0; }); cout << result << endl; return EXIT_SUCCESS; }
// 4 #include <algorithm> #include <cstdlib> #include <iostream> #include <iterator> #include <vector> using namespace std; int main() { vector<int> srcVec; for (int val = 0; val < 10; val++) { srcVec.push_back(val); } vector<double> destVec; transform(srcVec.begin(), srcVec.end(), back_inserter(destVec), [] (int _n) { if (_n < 5) return _n + 1.0; else if (_n % 2 == 0) return _n / 2.0; else return _n * _n; }); ostream_iterator<double> outIt(cout, " "); copy(destVec.begin(), destVec.end(), outIt); cout << endl; return EXIT_SUCCESS; }
"Error C3499: a lambda that has been defined as a void return type cannot return a value"
// 5 #include <algorithm> #include <cstdlib> #include <iostream> #include <iterator> #include <vector> using namespace std; int main() { vector<int> srcVec; for (int val = 0; val < 10; val++) { srcVec.push_back(val); } vector<double> destVec; transform(srcVec.begin(), srcVec.end(), back_inserter(destVec), [] (int _n) -> double { if (_n < 5) return _n + 1.0; else if (_n % 2 == 0) return _n / 2.0; else return _n * _n; }); ostream_iterator<double> outIt(cout, " "); copy(destVec.begin(), destVec.end(), outIt); cout << endl; return EXIT_SUCCESS; }
1 2 3 4 5 25 3 49 4 81
// 6 #include <algorithm> #include <cstdlib> #include <iostream> #include <iterator> #include <numeric> #include <vector> using namespace std; int main() { vector<int> srcVec; for (int val = 0; val < 10; val++) { srcVec.push_back(val); } int lowerBound = 0, upperBound = 0; cout << "Enter the value range: "; cin >> lowerBound >> upperBound; int result = count_if(srcVec.begin(), srcVec.end(), [lowerBound, upperBound] (int _n) { return lowerBound <= _n && _n < upperBound; }); cout << result << endl; return EXIT_SUCCESS; }
// 7 #include <algorithm> #include <cstdlib> #include <iostream> #include <iterator> #include <vector> using namespace std; class MyLambda { public: MyLambda(int _lowerBound, int _upperBound) : m_lowerBound(_lowerBound) , m_upperBound(_upperBound) {} bool operator ()(int _n) const { return m_lowerBound <= _n && _n < m_upperBound; } private: int m_lowerBound, m_upperBound; }; int main() { vector<int> srcVec; for (int val = 0; val < 10; val++) { srcVec.push_back(val); } int lowerBound = 0, upperBound = 0; cout << "Enter the value range: "; cin >> lowerBound >> upperBound; int result = count_if(srcVec.begin(), srcVec.end(), MyLambda(lowerBound, upperBound)); cout << result << endl; return EXIT_SUCCESS; }
// 8 #include <algorithm> #include <cstdlib> #include <iostream> #include <iterator> #include <numeric> #include <vector> using namespace std; int main() { vector<int> srcVec; int init = 0; generate_n(back_inserter(srcVec), 10, [init] () mutable { return init++; }); ostream_iterator<int> outIt(cout, " "); copy(srcVec.begin(), srcVec.end(), outIt); cout << endl << "init: " << init << endl; return EXIT_SUCCESS; }
0 1 2 3 4 5 6 7 8 9
init: 0
[] // [=] // [&] // [x, y] // x y [&x, &y] // x y [in, &out] // in , out — [=, &out1, &out2] // , out1 out2, // [&, x, &y] // , x…
// 9 #include <algorithm> #include <cstdlib> #include <iostream> #include <iterator> #include <vector> using namespace std; int main() { vector<int> srcVec; int init = 0; generate_n(back_inserter(srcVec), 10, [&] () mutable { return init++; }); ostream_iterator<int> outIt(cout, " "); copy(srcVec.begin(), srcVec.end(), outIt); cout << endl << "init: " << init << endl; return EXIT_SUCCESS; }
// 10 #include <algorithm> #include <cstdlib> #include <iostream> #include <iterator> #include <vector> using namespace std; class MyLambda { public: explicit MyLambda(int & _init) : init(_init) { } int operator ()() { return init++; } private: int & init; }; int main() { vector<int> srcVec; int init = 0; generate_n(back_inserter(srcVec), 10, MyLambda(init)); ostream_iterator<int> outIt(cout, " "); copy(srcVec.begin(), srcVec.end(), outIt); cout << endl << "init: " << init << endl; return EXIT_SUCCESS; }
0 1 2 3 4 5 6 7 8 9
init: 10
// 11 #include <algorithm> #include <cstdlib> #include <iostream> #include <iterator> #include <vector> using namespace std; class MyMegaInitializer { public: MyMegaInitializer(int _base, int _power) : m_val(_base) , m_power(_power) {} void initializeVector(vector<int> & _vec) { for_each(_vec.begin(), _vec.end(), [m_val, m_power] (int & _val) mutable { _val = m_val; m_val *= m_power; }); } private: int m_val, m_power; }; int main() { vector<int> myVec(11); MyMegaInitializer initializer(1, 2); initializer.initializeVector(myVec); return EXIT_SUCCESS; }
"Error C3480: 'MyMegaInitializer :: m_power': a lambda capture variable scope"
// 12 #include <algorithm> #include <cstdlib> #include <iostream> #include <iterator> #include <vector> using namespace std; class MyMegaInitializer { public: MyMegaInitializer(int _base, int _power) : m_val(_base) , m_power(_power) {} void initializeVector(vector<int> & _vec) { for_each(_vec.begin(), _vec.end(), [this] (int & _val) mutable { _val = m_val; m_val *= m_power; }); } private: int m_val, m_power; }; int main() { vector<int> myVec(11); MyMegaInitializer initializer(1, 2); initializer.initializeVector(myVec); for_each(myVec.begin(), myVec.end(), [] (int _val) { cout << _val << " "; }); cout << endl; return EXIT_SUCCESS; }
1 2 4 8 16 32 64 128 256 512 1024
[] (int _n) throw() { … }
[=] (const std::string & _str) mutable throw(std::bad_alloc) -> bool { … }
lambda-expression :: = '[' [<gripping_list>] ']' ['(' <list_of_parameters> ')' ['mutable']] ['noexcept'] ['->' <return type>] '{' [<lambda body>] '}'
// 13 #include <algorithm> #include <cstdlib> #include <functional> #include <iostream> #include <iterator> #include <vector> using namespace std; using std::tr1::function; int main() { vector<int> myVec; int init = 0; generate_n(back_inserter(myVec), 10, [&] { return init++; }); function<void (int)> traceLambda = [] (int _val) -> void { cout << _val << " "; }; for_each(myVec.begin(), myVec.end(), traceLambda); cout << endl; function<function<int (int)> (int)> lambdaGen = [] (int _val) -> function<int (int)> { return [_val] (int _n) -> int { return _n + _val; }; }; transform(myVec.begin(), myVec.end(), myVec.begin(), lambdaGen(2)); for_each(myVec.begin(), myVec.end(), traceLambda); cout << endl; return EXIT_SUCCESS; }
0 1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 10 11
transform(myVec.begin(), myVec.end(), myVec.begin(), bind2nd(plus<int>(), 2));
for (auto it = vec.begin(); it != vec.end(); ++it) { // ... }
// 14 #include <algorithm> #include <cstdlib> #include <functional> #include <iostream> #include <iterator> #include <vector> using namespace std; using std::tr1::function; int main() { vector<int> myVec; int init = 0; generate_n(back_inserter(myVec), 10, [&] { return init++; }); auto traceLambda = [] (int _val) -> void { cout << _val << " "; }; for_each(myVec.begin(), myVec.end(), traceLambda); cout << endl; auto lambdaGen = [] (int _val) -> function<int (int)> { return [_val] (int _n) -> int { return _n + _val; }; }; transform(myVec.begin(), myVec.end(), myVec.begin(), lambdaGen(2)); for_each(myVec.begin(), myVec.end(), traceLambda); cout << endl; return EXIT_SUCCESS; }
Source: https://habr.com/ru/post/66021/