C++,函子,特定任务/场景,将用户输入映射到函数

发布于 2024-11-18 21:14:15 字数 556 浏览 10 评论 0 原文

我已经了解函子的基础知识,它们是如何创建的等等。我现在有一个特定的代码,可能会受益于转换为函子:

它是关于控制台输入的。如果用户输入文本并按 Enter 键,则会尝试将输入的文本作为命令执行。目前可用的命令存储在 std::map 中,其中 map.first 是命令和映射的名称。第二个是指向包含该命令代码的函数的指针。

如果我现在将其转换为函子,最好的方法是什么?我是否可以简单地将函数指针替换为实际的函子对象?或者删除映射并简单地使用命令字符串调用仿函数(commandFunctor("command"))? 如果是第二种:这样的函子的最佳方式应该是什么样子?那么我是否应该只创建一个仿函数类,并在它的operator() 中放置一个if-else-if-else... 检查命令并将代码存储到其中?

或者我应该为每个命令创建一个新的函子类并调用与该命令对应的函子?

简而言之:用户输入命令。命令被查找。如果命令存在则执行其代码。如何使用函子以最有效的方式做到这一点?

I already know the basics about functors, how they are created and so on. I now have a specific code that would probably benefit from being converted to functors:

It's about a console input. If the user enters text and presses enter than it tries to execute the entered text as a command. Currently the available commands are stored in a std::map<std::string, (void*)(std::string)> where map.first is the name of the command and map.second is the pointer to a function containing the code for this command.

If I now convert this to functors, what would be the best way to do so? Would I simply replace the function pointer by the actual functor objects? Or would remove the map and simply call the functor with the command string (commandFunctor("command"))?
If the second: What would the best way such a functor should look like? Should I then only create ONE functor class and in it's operator() place a if-else-if-else... checking for the commands and store the code into it?

Or should I create a new functor class for each command and call the functor that corresponds to this command?

So, in short: User enters command. Command is looked up. If comnmand exists then execute its code. How to do this in the most efficietly way with functors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆沫 2024-11-25 21:14:15

我建议使用 std::map >。或者,如果您有权访问 C++0x 标准库,则可以使用 std::function 而不是 boost::function

boost/std::function 是一个可调用对象,它可以存储可使用您指定的函数类型调用的任何内容。因此,用户可以给您一个函数指针、他们创建的类型的仿函数,甚至是一个执行函数组合的 boost/std::bind 对象。

I would suggest using a std::map<std::string, boost::function<void(std::string)> >. Or, if you have access to a C++0x standard library, you can use std::function instead of boost::function.

A boost/std::function is a callable object that can store anything that is callable with the function type you specify. So the user can give you a function pointer, a functor of a type that they create, or even a boost/std::bind object that does function composition.

瀞厅☆埖开 2024-11-25 21:14:15

我会坚持使用您所拥有的一般形式,但使用 std::map 来存储命令(我正在使用 std ::tr1::function,但您可以使用任何适合的函子类)

I would stick with the general form of what you have, but use a std::map<std::string, std::tr1::function> to store the commands (i'm using std::tr1::function, but you can use whatever functor class fits)

孤城病女 2024-11-25 21:14:15

目前,您使用映射根据鉴别器(或字符串 id,或您想要的任何名称)来确定要调用哪个函数指针。将其替换为仅传递字符串的函子(例如 commandFunctor("command")),您只需将映射逻辑移至该函数即可。

您仍然需要映射,但正如您所注意到的,您绝对可以用函子替换函数指针,并且根据您的编译器/版本/等,您可以使用 std::tr1::function 或 boost,或其他否则你可能有。

Currently, you use the map to determine which function pointer to be called, based on the discriminator (or string id, or whatever you want to call it). Replacing it with a functor that just gets passed a string (say, commandFunctor("command")), you'll just have to move the mapping logic to that function.

You'll still need the map, but as you noticed, you can definitely replace the function pointer with a functor, and depending on your compiler/version/etc, you can use the std::tr1::function or boost, or whatever else you might have.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文