想要找到有效的方法来替换多个switch语句

问题描述 投票:0回答:2
while (1) {
    cout << "Enter the number corresponding to your choice:" << "\n";
    cout << "1 - Go North\n";
    cout << "2 - Go East\n";
    cout << "3 - Go West\n> ";
    cin >> choice;

    switch (choice){
      case 1:
        northAtlantic();
        break;
      case 2:
        southIndian();
        break;
      case 3:
        southPacific();
        break;
      default:
        cout << "Invalid choice. Try again.";
        pressEnter();
    }
  }
while (1) {
    cout << "Enter the number corresponding to your choice:" << "\n";
    cout << "1 - Go West\n";
    cout << "2 - Go South\n> ";
    cin >> choice;

    switch (choice){
      case 1:
        westArctic();
        break;
      case 2:
        westPacific();
        break;
      default:
        cout << "Invalid choice. Try again.";
        pressEnter();
    }
  }

我需要一种方法将这两个 switch 语句放入一个函数中,我可以调用该函数并将函数和选项放入参数中。但问题是我无法创建一个既接受 3 个 case 语句又接受 2 个 case 语句的函数,而且它没有任何意义。

我尝试创建一个既包含函数又包含选择的函数。我想要它,这样我就可以输入一个数字,它会做出很多 case 语句,然后我可以为这些语句输入文本和函数。

c++ switch-statement
2个回答
2
投票

两个函数之间不同的部分是字符串和关联函数。您可以使用

std::map
std::unorderd_map
,尽管您实际上想使用索引作为键来查找字符串,然后查找函数。因此
std::vector
就可以了。

void make_choice(const std::vector<std::string,const std::function<void()>& options) {
     while(1) {
         std::cout << "Enter the number corresponding to your choice:" << "\n";
         for (int i=0;i<options.size();++i) { 
             std::cout << i+1 << options[i].first << "\n";
         }
         int choice = 0;
         std::cin >> choice;
         if (choice > 0 && choice < options.size()) {
               options[i].second();
               break;
         } else {
               std::cout << "Invalid choice. Try again.";
               pressEnter();
         }
     }
}

为了进一步阅读,我建议您参考 cppreference,您可以在那里阅读有关

std::vector
std::function
的信息,因为我提到过它
std::map
std::unorderd_map

我想“高效”的意思是“高效写作”。运行时性能是次要的,因为大部分时间都花在等待用户输入一些输入上。

最后但并非最不重要的一点是,我没有添加比代码中更多的用户输入验证。您应该检查

int
的提取是否成功,因为如果失败(例如用户输入
foobar
),则上述内容将陷入无限循环。


0
投票

通过 a 对向量进行循环将允许您获得任意长度的菜单,并且设置起来很简单。

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <functional>
using namespace std;

using Items = vector< pair<function<void()>,string> >;     // Collection of functions with descriptions
//using Items = vector< pair<void(*)(),string> >;     // Alternative with simple function pointers

void northAtlantic(){ cout << "> northAtlantic\n\n"; }     // Whatever you do in quarter of an ocean!
void southIndian  (){ cout << "> southIndian  \n\n"; }
void southPacific (){ cout << "> southPacific \n\n"; }
void westArctic   (){ cout << "> westArctic   \n\n"; }
void westPacific  (){ cout << "> westPacific  \n\n"; }


void menu( const Items &items )
{
   int choice;
   while( true )
   {
      cout << "Enter the number corresponding to your choice (0 to exit):" << "\n";
      for ( int i = 1; i <= items.size(); i++ ) cout << i << " - " << items[i-1].second << '\n';
      cin >> choice;
      if ( choice == 0 ) return;
      if ( choice < 0 || choice > items.size() ) cout << "Invalid choice. Try again.\n\n";
      else                                       items[choice-1].first();
   }
}


int main()
{
   Items items1{ { northAtlantic, "Go North" }, { southIndian, "Go East"  }, { southPacific, "Go West" } };
   Items items2{ { westArctic   , "Go West"  }, { westPacific, "Go South" }                              };
   menu( items1 );
   menu( items2 );
}
© www.soinside.com 2019 - 2024. All rights reserved.