C ++:将枚举值打印为文本

如果我有这样的枚举


enum Errors

{ErrorA=0, ErrorB, ErrorC};

然后我要打印到控制台


Errors anError = ErrorA;

cout<<anError;/// 0 will be printed

但是我想要的是文本“ ErrorA”,我可以不使用if / switch来做到吗?

您对此有什么解决方案?


Helenr
浏览 635回答 3
3回答

临摹微笑

使用具有匹配值的字符串数组或向量:char *ErrorTypes[] ={&nbsp; &nbsp; "errorA",&nbsp; &nbsp; "errorB",&nbsp; &nbsp; "errorC"};cout << ErrorTypes[anError];编辑:以上解决方案适用于枚举是连续的,即从0开始且没有分配的值。它将与问题中的枚举完美配合。要进一步证明枚举不是从0开始的情况,请使用:cout << ErrorTypes[anError - ErrorA];

莫回无

这是一个基于Boost.Preprocessor的示例:#include <iostream>#include <boost/preprocessor/punctuation/comma.hpp>#include <boost/preprocessor/control/iif.hpp>#include <boost/preprocessor/comparison/equal.hpp>#include <boost/preprocessor/stringize.hpp>#include <boost/preprocessor/seq/for_each.hpp>#include <boost/preprocessor/seq/size.hpp>#include <boost/preprocessor/seq/seq.hpp>#define DEFINE_ENUM(name, values)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\&nbsp; enum name {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\&nbsp; &nbsp; BOOST_PP_SEQ_FOR_EACH(DEFINE_ENUM_VALUE, , values)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \&nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \&nbsp; inline const char* format_##name(name val) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \&nbsp; &nbsp; switch (val) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \&nbsp; &nbsp; &nbsp; BOOST_PP_SEQ_FOR_EACH(DEFINE_ENUM_FORMAT, , values)&nbsp; &nbsp; &nbsp; &nbsp;\&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\&nbsp; }#define DEFINE_ENUM_VALUE(r, data, elem)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \&nbsp; BOOST_PP_SEQ_HEAD(elem)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\&nbsp; BOOST_PP_IIF(BOOST_PP_EQUAL(BOOST_PP_SEQ_SIZE(elem), 2),&nbsp; &nbsp; &nbsp; \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= BOOST_PP_SEQ_TAIL(elem), )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\&nbsp; BOOST_PP_COMMA()#define DEFINE_ENUM_FORMAT(r, data, elem)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\&nbsp; case BOOST_PP_SEQ_HEAD(elem):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\&nbsp; return BOOST_PP_STRINGIZE(BOOST_PP_SEQ_HEAD(elem));DEFINE_ENUM(Errors,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((ErrorA)(0))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((ErrorB))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((ErrorC)))int main() {&nbsp; std::cout << format_Errors(ErrorB) << std::endl;}
打开App,查看更多内容
随时随地看视频慕课网APP