将char绑定到枚举类型 [英] Bind a char to an enum type

查看:215
本文介绍了将char绑定到枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class someclass 
{
public :
枚举{START,MID,END};
vector< Section>充分;
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;
}
}
}
};

现在想象我有更多的枚举类型,他们的名字更长....
我得到的不是一个非常好的代码,我想知道是否可能绑定一个特定的字符到枚举类型,可能会这样做:



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

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

解决方案

不幸的是,你可以做很多事情来清理它。如果您可以访问C ++ 11强类型枚举器功能,那么您可以执行以下操作:

 枚举类部分:char {
START ='S',
MID ='M',
END ='E',
};

然后,您可以执行以下操作:

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

但是,如果您无法访问此功能,那么您可以做的不多,我的建议将有一个全局地图 std :: map< Section,char> ,它将每个枚举一个字符或一个帮助函数与原型:

  inline char SectionToChar(Section section); 

其中只是实现了 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;
}
}
}


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?

解决方案

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:

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 enum section to a character, or a helper function with the prototype:

inline char SectionToChar( Section section );

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

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;
         }
     }
}

这篇关于将char绑定到枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆