Lambda funkce

{“https:\/\/www.jdoodle.com\/a\/cSW?format=text”:”#include <iostream>\n#include <vector>\nusing namespace std;\n\nvoid print(const vector<int>& vec) {\n for(auto val : vec)\n cout << val << ";";\n cout << endl;\n}\n\nauto lambdaPrint = [](const vector<int>& vec) {\n for(auto val : vec)\n cout << val << ";";\n cout << endl;\n};\n\nint main() {\n print({1, 2, 3 , 4, 5});\n lambdaPrint({5, 4, 3, 2, 1});\n return 0;\n}”,”https:\/\/www.jdoodle.com\/a\/cXy?format=text”:”#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nostream& operator<<(ostream& os, vector<int>& data) {\n for(auto c : data)\n os << c << ";";\n return os;\n}\n\nint main() {\n int start = 0;\n auto generator1 = [=]() {\n static int i = 0;\n return start + (++i);\n };\n auto generator2 = [&]() mutable { return ++start; };\n \n const int len = 10;\n vector<int> data(len);\n \n start = 5;\n generate_n(data.begin(), len, generator1);\n cout << "start = " << start << ", " << data << endl;\n \n start = 10;\n generate_n(data.begin(), len, generator2);\n cout << "start = " << start << ", " << data << endl;\n \n return 0;\n}”,”https:\/\/www.jdoodle.com\/a\/cYm?format=text”:”#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nostream& operator<<(ostream& os, vector<int>& vec) {\n for(auto val : vec)\n cout << val << ";";\n return os;\n}\n\nint main() {\n int value = 0;\n int increment = 1;\n bool isNegative = true;\n \n auto generator = [value, isNegative, &increment]() mutable {\n isNegative = ! isNegative;\n if(isNegative) {\n return -(value + increment++);\n } else {\n return value + increment++;\n }\n };\n \n const int len = 10;\n vector<int> data(len);\n generate_n(begin(data), len, generator);\n cout << data << endl;\n \n return 0;\n}”}


Lambda funkce
Ohodnoťte tento článek