Varianty sekvenčního algoritmu “find”

{“https:\/\/www.jdoodle.com\/a\/bSC?format=text”:”#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nvector<string> data = {\n "Ale\u0161", "Kl\u00e1ra", "Du\u0161an", "Dominik", "Honza"\n};\n\nvoid printIndex(vector<string>::iterator& it) {\n if(it != data.end())\n cout << "Index ve vektoru je " << distance(data.begin(), it) << endl;\n else\n cout << "Z\u00e1znam nenalezen." << endl;\n}\n\nint main() {\n auto it = find(data.begin(), data.end(), "Dominik");\n printIndex(it);\n it = find(data.begin(), data.end(), "V\u00e1clav");\n printIndex(it);\n it = find(data.begin(), data.end(), "Ale\u0161");\n printIndex(it);\n \n return 0;\n}”,”https:\/\/www.jdoodle.com\/a\/bSd?format=text”:”#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\nstruct Employee {\n string name;\n int wage;\n};\n\nvector<Employee> data = {\n { "Honza", 20000 },\n { "Alice", 35000 },\n { "Roman", 10000 },\n { "Kl\u00e1ra", 40000 },\n { "Ale\u0161", 15000 }\n};\n\nusing EmpIterator = typename vector<Employee>::iterator;\n\ntemplate<typename C> \nvoid find_all(C& container, function<typename C::iterator(typename C::iterator)> Op) {\n auto it = container.begin();\n while(it != container.end()) {\n it = Op(it);\n }\n}\n\ntemplate<typename Iterator>\nvoid safe_increment(Iterator& it, Iterator end) {\n if(it != end) {\n it++;\n }\n}\n\nvoid printEmployee(EmpIterator it) {\n if(it != data.end()) {\n cout << it->name << " m\u00e1 plat " << it->wage << endl;\n }\n}\n\nint main() {\n \/\/ Najdi v\u0161echny zam\u011bstnance s platem > 25000\n find_all(data, [](EmpIterator it) {\n it = find_if(it, data.end(), [](const Employee& e) { \n return e.wage > 25000; });\n printEmployee(it);\n safe_increment(it, data.end());\n return it;\n });\n \n cout << endl;\n \n \/\/ Vy\u0159a\u010f zam\u011bstnance s platem 15000 – 25000\n find_all(data, [](EmpIterator it) {\n it = find_if_not(it, data.end(), [](const Employee& e) { \n return e.wage >= 15000 && e.wage <= 25000; });\n printEmployee(it);\n safe_increment(it, data.end());\n return it;\n });\n \n return 0;\n}”,”https:\/\/www.jdoodle.com\/a\/bRZ?format=text”:”#include <iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nstring::difference_type charIndex(string& s, string::iterator& it) {\n return distance(s.begin(), it);\n}\n\nint main() {\n \/\/ Indexy: 01234567890123456789\n string s = "xxxFOO_BARabcd_FOOdd";\n string foo = "FOO";\n string bar = "BAR";\n \n auto it = find_first_of(s.begin(), s.end(), bar.begin(), bar.end());\n cout << "Index pro BAR je " << charIndex(s, it) << endl;\n \n it = find_end(s.begin(), s.end(), foo.begin(), foo.end());\n cout << "Index pro posledn\u00edho FOO je " << charIndex(s, it) << endl;\n \n return 0;\n}”,”https:\/\/www.jdoodle.com\/a\/bRW?format=text”:”#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nvector<int> sequence = {\n 1, 2, 3, 3, 3, 2, 4, 5, 6, 6, 6, 6, 6, 5, 4, 3, 2, 1, 1, 2 ,3\n};\n\ntemplate<typename Iterator>\nIterator printConstantRegion(Iterator it, Iterator end) {\n typename Iterator::value_type value = *it;\n while(it != end) {\n if(*it != value)\n break;\n cout << *it << ";";\n it++;\n }\n cout << endl;\n return it;\n}\n\nint main() {\n auto it = sequence.begin();\n while(it != sequence.end()) {\n it = adjacent_find(it, sequence.end());\n if(it != sequence.end())\n it = printConstantRegion(it, sequence.end());\n }\n}”}


Varianty sekvenčního algoritmu “find”
Ohodnoťte tento článek