字符串的枚举的等效项
我研究了枚举,它只需要整数输入并返回相应的值。我想实现同样的目标,但我只有字符串作为输入。我想做以下工作 -
enum Types {
"Absolute", //"abs"
"PURE", //"PRE"
"MIXED" //"MXD"
}
可能的陈述可能是 -
string sTpes = Types("abs"); //this should return "Absolute"
或者
string sTpes = Types("MXD"); //this should return "MIXED"
如果不使用枚举,请建议我实现这一目标的可能方法。
谢谢。
I studied enums which expects only integer inputs and returns corresponding value to it.I want to achieve same thing but I only have strings as a input. I want to make following work -
enum Types {
"Absolute", //"abs"
"PURE", //"PRE"
"MIXED" //"MXD"
}
and probable statment could be -
string sTpes = Types("abs"); //this should return "Absolute"
or
string sTpes = Types("MXD"); //this should return "MIXED"
If not using enums, please suggest me possible ways to achieve this.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 string.hi 中的字符串数组(大小为 2)(可以是该数组,也可以只是字符串;一个用于 C,另一个用于 cpp)。第一个字符串是“abs”,第二个字符串是“absolute”。
例如:
you could use a string array (of size 2) from string.h i think (either that or just string; one is for C and other is for cpp). first string is "abs" second is "absolute".
for example:
没有“字符串枚举”,但要从一个值映射到另一个值,您可以使用 std::map ,这是 C++ 平台附带的标准模板:
在 C++0x 中,如果如果您想要“安全”访问,如果找不到键类型,则抛出异常,请使用
map::at
(实际上,缺少map::at
> 只是当前标准中的一个疏忽):或检查它是否存在:
There are no "string-enums", but to map from one value to another, you can use
std::map
, which is a standard template shipped with C++ platforms:In C++0x, if you want "safe" access that throws an exception if the key-type wasn't found, use
map::at
(actually, afair, the lack ofmap::at
was just an oversight in the current standard):or check if it exists:
如果你正在谈论 c++/cli 你可以使用这个
Hashtable^ openWith = gcnew Hashtable();
来自 http://msdn.microsoft.com/fr- fr/library/system.collections.hashtable.aspx#Y4406
否则使用 stdlib 中的映射。
我认为您也可以从 MFC 使用 CMAP,这里有一篇关于它的好文章: http: //www.codeproject.com/KB/architecture/cmap_howto.aspx
if you are talking about c++/cli you could use this
Hashtable^ openWith = gcnew Hashtable();
from http://msdn.microsoft.com/fr-fr/library/system.collections.hashtable.aspx#Y4406
else use map from stdlib.
I think you can also use CMAP from MFC, there is a good article about it here : http://www.codeproject.com/KB/architecture/cmap_howto.aspx
enum
具有整数值。我个人只是建议两个转换函数:enum ->字符串
字符串 -> enum
第一个可以使用简单的数组来实现,第二个需要在排序列表中进行二分搜索。
An
enum
has an integral value. Personally I simply suggest two conversion functions:enum -> string
string -> enum
The first can be implemented with a simple array, the second require a binary search in a sorted list.