字符串的枚举的等效项

发布于 2024-11-28 23:58:34 字数 400 浏览 2 评论 0原文

我研究了枚举,它只需要整数输入并返回相应的值。我想实现同样的目标,但我只有字符串作为输入。我想做以下工作 -

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 技术交流群。

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

发布评论

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

评论(4

雪落纷纷 2024-12-05 23:58:35

您可以使用 string.hi 中的字符串数组(大小为 2)(可以是该数组,也可以只是字符串;一个用于 C,另一个用于 cpp)。第一个字符串是“abs”,第二个字符串是“absolute”。

例如:

#include <string>

...

string abs[2]; //or a better name that's more relevant to you

abs[0] = "abs";
abs[1] = "absolute";

...

//pass it into the function
cout << abs[1] << endl;

...

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:

#include <string>

...

string abs[2]; //or a better name that's more relevant to you

abs[0] = "abs";
abs[1] = "absolute";

...

//pass it into the function
cout << abs[1] << endl;

...
笑咖 2024-12-05 23:58:34

没有“字符串枚举”,但要从一个值映射到另一个值,您可以使用 std::map ,这是 C++ 平台附带的标准模板:

#include <map>
#include <string>

int main() {
    using std::map;  using std::string;

    map<string, string> ss;
    ss["abs"] = "Absolute";

    const string foo = ss["abs"];
    std::cout << ss["abs"] << ", or " << foo << std::endl;
}

在 C++0x 中,如果如果您想要“安全”访问,如果找不到键类型,则抛出异常,请使用 map::at (实际上,缺少 map::at > 只是当前标准中的一个疏忽):

    std::cout << ss.at("weird keY");

或检查它是否存在:

    if (ss.find("weird keY")==ss.end())
        std::cout << "key not found\n";

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:

#include <map>
#include <string>

int main() {
    using std::map;  using std::string;

    map<string, string> ss;
    ss["abs"] = "Absolute";

    const string foo = ss["abs"];
    std::cout << ss["abs"] << ", or " << foo << std::endl;
}

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 of map::at was just an oversight in the current standard):

    std::cout << ss.at("weird keY");

or check if it exists:

    if (ss.find("weird keY")==ss.end())
        std::cout << "key not found\n";
黎歌 2024-12-05 23:58:34

如果你正在谈论 c++/cli 你可以使用这个
Hashtable^ openWith = gcnew Hashtable();

    // Add some elements to the hash table. There are no
    // duplicate keys, but some of the values are duplicates.
    openWith->Add("txt", "notepad.exe");
    openWith->Add("bmp", "paint.exe");
    openWith->Add("dib", "paint.exe");
    openWith->Add("rtf", "wordpad.exe");

来自 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();

    // Add some elements to the hash table. There are no
    // duplicate keys, but some of the values are duplicates.
    openWith->Add("txt", "notepad.exe");
    openWith->Add("bmp", "paint.exe");
    openWith->Add("dib", "paint.exe");
    openWith->Add("rtf", "wordpad.exe");

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

红衣飘飘貌似仙 2024-12-05 23:58:34

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.

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