online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
// Типа тут подумал о расширяемости и накидал всякого #include <iostream> #include <string_view> #include <vector> #include <functional> using namespace std; // тут одностраничник // Types.xxxx using Id = size_t; using Ids = vector<Id>; using Refrigerator = string_view; using Refrigerators = vector<string_view>; using Strategy = function<bool(string_view)>; //------------------------------------------------------------------------------ // Strategies.xxxx // Стратегия по любому тегу class InfectedByTag{ public: explicit InfectedByTag(string virus_tag): m_virus_tag(std::move(virus_tag)) {} bool operator()(const Refrigerator refrigerator_to_verify){ if(refrigerator_to_verify.size() < m_virus_tag.size()) return false; size_t tag_index = 0; for(const auto& s: refrigerator_to_verify){ if(s == m_virus_tag[tag_index]) ++tag_index; if(tag_index == m_virus_tag.size()) return true; } return false; } private: const string m_virus_tag; }; //------------------------------------------------------------------------------ // Checkers.xxxx // Параметризируется любой стратегией Ids CheckRefrigerators(Refrigerators refrigerators_to_verify, Strategy strategy){ if(refrigerators_to_verify.empty()) return {}; Ids infected_ids; for(Id id = 0; id < refrigerators_to_verify.size(); ++id){ if(strategy(refrigerators_to_verify[id])) infected_ids.push_back(id); } return infected_ids; } //------------------------------------------------------------------------------ // Utils.xxxx void PrintIds(const Ids& infected_ids){ for(auto&& id: infected_ids) { cout << id << endl; } } //------------------------------------------------------------------------------ int main() { // Немного мыслей: // - "annton" тоже является верным решением, если судить по скрину и прилагаемым юзер кейсам // - Возможнро есть убер решения через xor и биты, но мне лень рассматривать эти решения. // Типа accummulate(cbegin(refrigerator), cend(refrigerator), "anton", [](char letter){ /*битово-байтаёбская магия*/} ); // - Для массива из строчных литер (array<const char*>) наверное можно сделать compile-time версию. // - Хотел сделать не функтор, а шаблон типа InfectedByTag<virus_tag> но походу просто шаблон к std::funtion не привести =( const string virus_tag = "anton"; // Допустим у нас есть модуль со стратегиями и мы хотим их использовать const auto infectedIds = CheckRefrigerators({"anton", "1a1n1t1o1n1", "annton", "antn"}, InfectedByTag(virus_tag)); // Или в конкретном случае использовать лямбду const auto infectedIds2 = CheckRefrigerators({"anton", "1a1n1t1o1n1", "annton", "antn"}, [&virus_tag](const string_view refrigerator_to_verify)->bool { if(refrigerator_to_verify.size() < virus_tag.size()) return false; size_t tag_index = 0; for(const auto& s: refrigerator_to_verify){ if(s == virus_tag[tag_index]) ++tag_index; if(tag_index == virus_tag.size()) return true; } return false; } ); PrintIds(infectedIds); PrintIds(infectedIds2); return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue