C++:形成不同数据类型集合的适当解决方案是什么?
我正在编写一个命令行解释器,并且正在尝试为各个命令设置格式。 我有命令名称、最大参数数量和最小参数数量等信息。 我想要某种集合,一种参数类型的原型。 我的第一个想法是声明一个没有泛型的向量,但后来我意识到这不是 Java。
假设我有一个命令,例如“read test.dat 2” 我想要一个结构,显示典型的读取命令有一个字符串,然后是一个整数。
有任何想法吗?
I'm writing a command line interpreter and I'm trying to setup formats for individual commands. I have things like the name of the command, the maximum amount of parameters and the minimum amount of parameters. I want to have sort of collection, a sort of prototype of what kind of types the parameters are. My first thought was just to declare a vector without the generics, but then I realized this isn't Java.
Let's say I have a command such as "read test.dat 2"
I would want a structure showing that the typical read command has a string and then an integer.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不太清楚你在问什么,所以我可能会弄错。
从您的描述来看,听起来好像您有一个命令的抽象概念,并且它们有名称和参数方面的预期结构。 根据您的描述,听起来您需要一个类型标识符列表以及它们是否可选的指示。
然后,您可以为您希望使用的每个命令实例化一个命令对象,并将它们全部添加到命令集合中。
或者,使用映射集合将命令名称映射到实际的命令对象。
每个命令对象还可以保存对处理程序对象的引用以实际执行命令。
I'm not really clear about what you are asking so I may be getting this wrong.
From your description ,it sounds as if you have an abstract notion of commands, and that they have names and an expected structure in terms of parameters. From your description, it sounds like you would want a list of type identifiers and indications on whether they are optional.
Then, you could just instantiate a command object for each command you expect to work with, and add all of them to a collection of commands.
Alternatively, use a map collection to map from command names to the actual command objects.
Each command object can also hold a reference to a handler object to actually execute the command.
异构容器
如果您想要一个可以存储一组固定类型的容器,您可以使用boost::variant 之一:
现在,您可以将字符串或整数push_back 到向量中,并且变体将注明它包含的内容:
您可以在其文档中阅读相关内容 Boost 变体,包括如何从变体中获取正确的值。 如果您已经设置了命令行解析,并且不再需要库,您当然也可以拥有从参数名称到此类变体的映射:
命令行解析
如果您想解析您的命令行参数程序,您可以查看 Boost.Program 选项 图书馆,这将极大地帮助你做到这一点。
然而,大多数情况下,我最终使用 posix getopt 函数,它也可以解析命令行。 我建议你先看看boost程序选项,如果你觉得它太重了,你可以看看getopt(参见
man 3 getopt
)Heterogenous Container
If you want to have a container that can store a fixed set of types, you can use one of
boost::variant
:Now, you can push_back into the vector either strings or integers, and the variant will note what it contains:
You can read about it in its documentation at Boost variant, including how to get the right value out of the variant. You can of course also have a map from argument names to such variants, if you have already set up your command line parsing, and don't need libraries for that anymore:
Command line parsing
If you want to parse the command line arguments of your program, you can look into the Boost.Program Options library, which will greatly assist you doing that.
Mostly, however, i end up using the posix
getopt
function, which can also parse the command line. I recommend you to look into boost program options first, and if you feel it's too heavy, you can look into getopt (seeman 3 getopt
)也许是这样的:
Something like this, perhaps: