C++ 将字符绑定到枚举类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17095639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:55:20  来源:igfitidea点击:

Bind a char to an enum type

c++coding-styleenumschar

提问by Ravid Goldenberg

I have a piece of code pretty similar to this:

我有一段与此非常相似的代码:

class someclass
{
public:
enum Section{START,MID,END};
vector<Section> Full;
void ex(){
    for(int i=0;i<Full.size();i++)
    {
        switch (Full[i])
        {
        case START :
                  cout<<"S";
                  break;
        case MID :
                  cout<<"M";
                  break;
        case END:
            cout<<"E";
            break;
        }
    }
    }
};

Now imagine I have much more enum types and their names are longer.... well what i get is not a very good looking code and i was wondering if it possible to bind a specific char to an enum type and maybe do something like this:

现在想象我有更多的枚举类型,它们的名字更长......好吧,我得到的不是一个很好看的代码,我想知道是否可以将特定的字符绑定到枚举类型,也许可以做这样的事情:

for(int i=0;i<Full.size();i++)
    {
        cout<(Full[i]).MyChar();
    }

Or any other method that could make this code "prettier". Is this possible?

或者任何其他可以使此代码“更漂亮”的方法。这可能吗?

回答by Thomas Russell

Unfortunately there is not much you can do to clean this up. If you have access to the C++11 strongly typed enumerator feature, then you could do something like the following:

不幸的是,您无能为力来清理它。如果您有权访问 C++11 强类型枚举器功能,那么您可以执行以下操作:

enum class Section : char {
     START = 'S',
     MID = 'M',
     END = 'E',
};

And then you could do something like:

然后你可以做这样的事情:

std::cout << static_cast<char>(Full[i]) << std::endl;

However, if you do not have access to this feature then there's not much you can do, my advice would be to have either a global map std::map<Section, char>, which relates each enumsection to a character, or a helper function with the prototype:

但是,如果您无法访问此功能,那么您无能为力,我的建议是拥有一个全局 map std::map<Section, char>,它将每个enum部分与一个角色相关联,或者拥有一个带有原型的辅助函数:

inline char SectionToChar( Section section );

Which just implements the switch()statement in a more accessible way, e.g:

它只switch()是以更易于访问的方式实现语句,例如:

inline char SectionToChar( Section section ) {
     switch( section )
     {
     default:
         {
             throw std::invalid_argument( "Invalid Section value" );
             break;
         }
     case START:
         {
             return 'S';
             break;
         }
     case MID:
         {
             return 'M';
             break;
         }
     case END:
         {
             return 'E';
             break;
         }
     }
}

回答by Seth Hays

In a situation like this you could be tricky and cast your chars.

在这种情况下,您可能会很棘手并使用字符。

enum Section{
    START = (int)'S',
    MID   = (int)'M',
    END   = (int)'E'
};

...

inline char getChar(Section section)
{
    return (char)section;
}

回答by Forest Kunecke

I think the best solution in this case would be to use a map:

我认为在这种情况下最好的解决方案是使用地图:

#include <iostream>
#include <map>
class someclass
{
    public:
    enum Section{START = 0,MID,END};
    map<Section,string> Full;

    // set using Full[START] = "S", etc

    void ex(){
        for(int i=0;i<Full.size();i++)
        {
            cout << Full[i];
        }
    }
};