C ++中的简单文本菜单 [英] Simple text menu in C++
问题描述
我正在用C ++编写一个愚蠢的小应用程序来测试我的一个库.我希望该应用程序向用户显示命令列表,允许用户键入命令,然后执行与该命令关联的操作.听起来很简单. 在C#中,我最终将像这样编写命令列表/映射:
I am writing a silly little app in C++ to test one of my libraries. I would like the app to display a list of commands to the user, allow the user to type a command, and then execute the action associated with that command. Sounds simple enough. In C# I would end up writing a list/map of commands like so:
class MenuItem
{
public MenuItem(string cmd, string desc, Action action)
{
Command = cmd;
Description = desc;
Action = action;
}
public string Command { get; private set; }
public string Description { get; private set; }
public Action Action { get; private set; }
}
static void Main(string[] args)
{
var items = new List<MenuItem>();
items.Add(new MenuItem(
"add",
"Adds 1 and 2",
()=> Console.WriteLine(1+2)));
}
关于如何在C ++中实现此目标的任何建议?我真的不想为每个命令定义单独的类/函数.我可以使用Boost,但不能使用TR1.
Any suggestions on how to achieve this in C++? I don't really want to define separate classes/functions for each command. I can use Boost, but not TR1.
推荐答案
一种非常常见的技术是使用按项目名称索引的函数指针或boost :: function,或者使用它们的向量并按索引进行索引.这项工作的项目索引.使用项目名称的简单示例:
A very common technique is to use function pointers, or boost::function, indexed by the item name, or by having a vector of them and indexing by the item index for this job. Simple example using the item name:
void exit_me(); /* exits the program */
void help(); /* displays help */
std::map< std::string, boost::function<void()> > menu;
menu["exit"] = &exit_me;
menu["help"] = &help;
std::string choice;
for(;;) {
std::cout << "Please choose: \n";
std::map<std::string, boost::function<void()> >::iterator it = menu.begin();
while(it != menu.end()) {
std::cout << (it++)->first << std::endl;
}
std::cin >> choice;
if(menu.find(choice) == menu.end()) {
/* item isn't found */
continue; /* next round */
}
menu[choice](); /* executes the function */
}
C ++还没有lambda功能,因此遗憾的是,您确实必须使用函数来完成此任务.您可以使用boost :: lambda,但是请注意,它只是在模拟lambdas,远没有本地解决方案那么强大:
C++ doesn't have a lambda feature yet, so you really have to use functions for this task, sadly. You can use boost::lambda, but note it is just simulating lambdas, and nowhere near as powerful as a native solution:
menu["help"] = cout << constant("This is my little program, you can use it really nicely");
请注意使用constant(...),因为否则boost :: lambda不会注意到这应该是lambda表达式:编译器将尝试使用std :: cout输出字符串,并进行赋值结果(一个std :: ostream引用)到menu ["help"].您仍然可以使用boost :: function,因为它将接受所有返回void并且不带参数的东西-包括boost :: lambda创建的函数对象.
Note the use of constant(...), since otherwise boost::lambda wouldn't notice that this is supposed to be a lambda expression: The compiler would try to output the string using std::cout, and assign the result (an std::ostream reference) to menu["help"]. You can still use boost::function, since it will accept everything returning void and taking no arguments - including function objects, which is what boost::lambda creates.
如果您确实不想要单独的功能或boost :: lambda,则只需打印出项目名称的矢量,然后在用户指定的项目编号上打印switch
即可.这可能是最简单,最直接的方法.
If you really don't want separate functions or boost::lambda, you can just take print out a vector of the item names, and then switch
on the item number given by the user. This is probably the easiest and most straight forward way of doing it.
这篇关于C ++中的简单文本菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!