Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - #include <iostream>
 - #include <vector>
 - #include <string>
 - #include <functional>
 - class ConsoleMenu {
 - private:
 - struct MenuItem {
 - std::string label;
 - std::function<void()> action;
 - ConsoleMenu subMenu;
 - };
 - std::vector<MenuItem> items;
 - public:
 - void addItem(const std::string& label, const std::function<void()>& action) {
 - items.push_back({ label, action, {} });
 - }
 - ConsoleMenu& addSubMenu(const std::string& label) {
 - items.push_back({ label, {}, {} });
 - return items.back().subMenu;
 - }
 - void show() const {
 - while (true) {
 - // Выводим список опций
 - std::cout << "Выберите опцию:" << std::endl;
 - for (int i = 0; i < items.size(); i++) {
 - std::cout << i + 1 << ") " << items[i].label << std::endl;
 - }
 - // Считываем номер выбранной опции
 - int choice;
 - std::cin >> choice;
 - // Выполняем действие, соответствующее выбранной опции
 - if (choice >= 1 && choice <= items.size()) {
 - const auto& selectedItem = items[choice - 1];
 - if (selectedItem.action) {
 - // Это обычная опция
 - selectedItem.action();
 - } else {
 - // Это подменю
 - selectedItem.subMenu.show();
 - }
 - } else {
 - std::cout << "Неверный выбор" << std::endl;
 - }
 - }
 - }
 - };
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment