Algoritmy “transform” a “replace”

{“https:\/\/www.jdoodle.com\/a\/clV?format=text”:”#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\ntemplate<typename C>\nvoid print(C container, const char separator=’;’) {\n for(auto c : container)\n cout << c << separator;\n cout << endl;\n}\n\nint main() {\n auto generator = []() {\n static int val = 0;\n return ++val;\n };\n vector<int> data;\n generate_n(back_inserter(data), 10, generator);\n \n auto transformation = [](int item) {\n string s;\n fill_n(back_inserter(s), item, ‘x’);\n return s;\n };\n vector<string> transformedData;\n transform(data.begin(), data.end(), back_inserter(transformedData), transformation);\n \n print(data);\n print(transformedData, ‘\\n’);\n return 0;\n}”,”https:\/\/www.jdoodle.com\/a\/cmL?format=text”:”#include <iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n string s = "Hello, world !!!!";\n cout << s << endl;\n replace(s.begin(), s.end(), ‘l’, ‘_’);\n cout << s << endl;\n}”,”https:\/\/www.jdoodle.com\/a\/cms?format=text”:”#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\ntemplate<typename C>\nvoid print(C c) {\n for(auto val : c)\n cout << val << ";";\n cout << endl;\n}\n\nint main() {\n vector<int> data;\n auto generator = []() {\n static int val = 0;\n return ++val;\n };\n generate_n(back_inserter(data), 20, generator);\n \n print(data);\n replace_if(data.begin(), data.end(), [](int item) { return item % 2 == 0; }, 0);\n print(data);\n return 0;\n}”,”https:\/\/www.jdoodle.com\/a\/cmO?format=text”:”#include <iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n string s = "Hello, world !!!!";\n string s2;\n replace_copy(s.begin(), s.end(), back_inserter(s2), ‘l’, ‘_’);\n cout << s << endl;\n cout << s2 << endl;\n}”,”https:\/\/www.jdoodle.com\/a\/cmH?format=text”:”#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\ntemplate<typename C>\nvoid print(C c) {\n for(auto val : c)\n cout << val << ";";\n cout << endl;\n}\n\nint main() {\n vector<int> data;\n auto generator = []() {\n static int val = 0;\n return ++val;\n };\n generate_n(back_inserter(data), 20, generator);\n \n vector<int> data2;\n replace_copy_if(data.begin(), data.end(), back_inserter(data2), \n [](int item) { return item % 3 == 0; }, -1);\n print(data);\n print(data2);\n return 0;\n}”}


Algoritmy “transform” a “replace”
Ohodnoťte tento článek