C++:将枚举值打印为文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3342726/
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
C++: Print out enum value as text
提问by tiboo
If i have an enum like this
如果我有这样的枚举
enum Errors
{ErrorA=0, ErrorB, ErrorC};
Then i want to print out to console
然后我想打印到控制台
Errors anError = ErrorA;
cout<<anError;/// 0 will be printed
but what i want is the text "ErrorA", can i do it without using if/switch?
And what is your solution for this?
但我想要的是文本“ErrorA”,我可以在不使用 if/switch 的情况下做到吗?
你的解决方案是什么?
采纳答案by SigTerm
Using map:
使用地图:
#include <iostream>
#include <map>
#include <string>
enum Errors {ErrorA=0, ErrorB, ErrorC};
std::ostream& operator<<(std::ostream& out, const Errors value){
static std::map<Errors, std::string> strings;
if (strings.size() == 0){
#define INSERT_ELEMENT(p) strings[p] = #p
INSERT_ELEMENT(ErrorA);
INSERT_ELEMENT(ErrorB);
INSERT_ELEMENT(ErrorC);
#undef INSERT_ELEMENT
}
return out << strings[value];
}
int main(int argc, char** argv){
std::cout << ErrorA << std::endl << ErrorB << std::endl << ErrorC << std::endl;
return 0;
}
Using array of structures with linear search:
使用具有线性搜索的结构数组:
#include <iostream>
#include <string>
enum Errors {ErrorA=0, ErrorB, ErrorC};
std::ostream& operator<<(std::ostream& out, const Errors value){
#define MAPENTRY(p) {p, #p}
const struct MapEntry{
Errors value;
const char* str;
} entries[] = {
MAPENTRY(ErrorA),
MAPENTRY(ErrorB),
MAPENTRY(ErrorC),
{ErrorA, 0}//doesn't matter what is used instead of ErrorA here...
};
#undef MAPENTRY
const char* s = 0;
for (const MapEntry* i = entries; i->str; i++){
if (i->value == value){
s = i->str;
break;
}
}
return out << s;
}
int main(int argc, char** argv){
std::cout << ErrorA << std::endl << ErrorB << std::endl << ErrorC << std::endl;
return 0;
}
Using switch/case:
使用开关/外壳:
#include <iostream>
#include <string>
enum Errors {ErrorA=0, ErrorB, ErrorC};
std::ostream& operator<<(std::ostream& out, const Errors value){
const char* s = 0;
#define PROCESS_VAL(p) case(p): s = #p; break;
switch(value){
PROCESS_VAL(ErrorA);
PROCESS_VAL(ErrorB);
PROCESS_VAL(ErrorC);
}
#undef PROCESS_VAL
return out << s;
}
int main(int argc, char** argv){
std::cout << ErrorA << std::endl << ErrorB << std::endl << ErrorC << std::endl;
return 0;
}
回答by Igor Oks
Use an array or vector of strings with matching values:
使用具有匹配值的字符串数组或向量:
char *ErrorTypes[] =
{
"errorA",
"errorB",
"errorC"
};
cout << ErrorTypes[anError];
EDIT: The solution above is applicable when the enum is contiguous, i.e. starts from 0 and there are no assigned values. It will work perfectly with the enum in the question.
编辑:当枚举是连续的,即从 0 开始并且没有指定值时,上面的解决方案是适用的。它将与问题中的枚举完美配合。
To further proof it for the case that enum doesn't start from 0, use:
为了进一步证明枚举不是从 0 开始的情况,请使用:
cout << ErrorTypes[anError - ErrorA];
回答by Philipp
Here is an example based on Boost.Preprocessor:
下面是一个基于 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) \
enum name { \
BOOST_PP_SEQ_FOR_EACH(DEFINE_ENUM_VALUE, , values) \
}; \
inline const char* format_##name(name val) { \
switch (val) { \
BOOST_PP_SEQ_FOR_EACH(DEFINE_ENUM_FORMAT, , values) \
default: \
return 0; \
} \
}
#define DEFINE_ENUM_VALUE(r, data, elem) \
BOOST_PP_SEQ_HEAD(elem) \
BOOST_PP_IIF(BOOST_PP_EQUAL(BOOST_PP_SEQ_SIZE(elem), 2), \
= BOOST_PP_SEQ_TAIL(elem), ) \
BOOST_PP_COMMA()
#define DEFINE_ENUM_FORMAT(r, data, elem) \
case BOOST_PP_SEQ_HEAD(elem): \
return BOOST_PP_STRINGIZE(BOOST_PP_SEQ_HEAD(elem));
DEFINE_ENUM(Errors,
((ErrorA)(0))
((ErrorB))
((ErrorC)))
int main() {
std::cout << format_Errors(ErrorB) << std::endl;
}
回答by jxh
You can use a simpler pre-processor trick if you are willing to list your enum
entries in an external file.
如果您愿意enum
在外部文件中列出您的条目,您可以使用更简单的预处理器技巧。
/* file: errors.def */
/* syntax: ERROR_DEF(name, value) */
ERROR_DEF(ErrorA, 0x1)
ERROR_DEF(ErrorB, 0x2)
ERROR_DEF(ErrorC, 0x4)
Then in a source file, you treat the file like an include file, but you define what you want the ERROR_DEF
to do.
然后在源文件中,您将该文件视为包含文件,但您可以定义要ERROR_DEF
执行的操作。
enum Errors {
#define ERROR_DEF(x,y) x = y,
#include "errors.def"
#undef ERROR_DEF
};
static inline std::ostream & operator << (std::ostream &o, Errors e) {
switch (e) {
#define ERROR_DEF(x,y) case y: return o << #x"[" << y << "]";
#include "errors.def"
#undef ERROR_DEF
default: return o << "unknown[" << e << "]";
}
}
If you use some source browsing tool (like cscope), you'll have to let it know about the external file.
如果您使用某些源浏览工具(如 cscope),则必须让它知道外部文件。
回答by Mark Ingram
I use a string array whenever I define an enum:
每当我定义枚举时,我都会使用字符串数组:
Profile.h
配置文件.h
#pragma once
struct Profile
{
enum Value
{
Profile1,
Profile2,
};
struct StringValueImplementation
{
const wchar_t* operator[](const Profile::Value profile)
{
switch (profile)
{
case Profile::Profile1: return L"Profile1";
case Profile::Profile2: return L"Profile2";
default: ASSERT(false); return NULL;
}
}
};
static StringValueImplementation StringValue;
};
Profile.cpp
配置文件.cpp
#include "Profile.h"
Profile::StringValueImplementation Profile::StringValue;
回答by Nordic Mainframe
There has been a discussion here which might help: Is there a simple way to convert C++ enum to string?
这里有一个讨论可能会有所帮助:Is there a simple way to convert C++ enum to string?
UPDATE:Here#s a script for Lua which creates an operator<< for each named enum it encounters. This might need some work to make it work for the less simple cases [1]:
更新:这里#sa 脚本为 Lua 创建一个 operator<< 为它遇到的每个命名枚举。这可能需要一些工作才能使其适用于不太简单的情况 [1]:
function make_enum_printers(s)
for n,body in string.gmatch(s,'enum%s+([%w_]+)%s*(%b{})') do
print('ostream& operator<<(ostream &o,'..n..' n) { switch(n){')
for k in string.gmatch(body,"([%w_]+)[^,]*") do
print(' case '..k..': return o<<"'..k..'";')
end
print(' default: return o<<"(invalid value)"; }}')
end
end
local f=io.open(arg[1],"r")
local s=f:read('*a')
make_enum_printers(s)
Given this input:
鉴于此输入:
enum Errors
{ErrorA=0, ErrorB, ErrorC};
enum Sec {
X=1,Y=X,foo_bar=X+1,Z
};
It produces:
它产生:
ostream& operator<<(ostream &o,Errors n) { switch(n){
case ErrorA: return o<<"ErrorA";
case ErrorB: return o<<"ErrorB";
case ErrorC: return o<<"ErrorC";
default: return o<<"(invalid value)"; }}
ostream& operator<<(ostream &o,Sec n) { switch(n){
case X: return o<<"X";
case Y: return o<<"Y";
case foo_bar: return o<<"foo_bar";
case Z: return o<<"Z";
default: return o<<"(invalid value)"; }}
So that's probably a start for you.
所以这对你来说可能是一个开始。
[1] enums in different or non-namespace scopes, enums with initializer expressions which contain a komma, etc.
[1] 不同或非命名空间范围内的枚举,带有包含 komma 的初始化表达式的枚举等。
回答by Vladimir Chernyshev
#include <iostream>
using std::cout;
using std::endl;
enum TEnum
{
EOne,
ETwo,
EThree,
ELast
};
#define VAR_NAME_HELPER(name) #name
#define VAR_NAME(x) VAR_NAME_HELPER(x)
#define CHECK_STATE_STR(x) case(x):return VAR_NAME(x);
const char *State2Str(const TEnum state)
{
switch(state)
{
CHECK_STATE_STR(EOne);
CHECK_STATE_STR(ETwo);
CHECK_STATE_STR(EThree);
CHECK_STATE_STR(ELast);
default:
return "Invalid";
}
}
int main()
{
int myInt=12345;
cout << VAR_NAME(EOne) " " << VAR_NAME(myInt) << endl;
for(int i = -1; i < 5; i)
cout << i << " " << State2Str((TEnum)i) << endl;
return 0;
}
回答by MrPickles7
This is a good way,
这是个好办法,
enum Rank { ACE = 1, DEUCE, TREY, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, Hyman, QUEEN, KING };
Print it with an array of character arrays
用字符数组数组打印它
const char* rank_txt[] = {"Ace", "Deuce", "Trey", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Hyman", "Four", "King" } ;
Like this
像这样
std::cout << rank_txt[m_rank - 1]
回答by Adrian Regan
You could use a stl map container....
您可以使用 stl 地图容器....
typedef map<Errors, string> ErrorMap;
ErrorMap m;
m.insert(ErrorMap::value_type(ErrorA, "ErrorA"));
m.insert(ErrorMap::value_type(ErrorB, "ErrorB"));
m.insert(ErrorMap::value_type(ErrorC, "ErrorC"));
Errors error = ErrorA;
cout << m[error] << endl;
回答by Johan Kotlinski
For this problem, I do a help function like this:
对于这个问题,我做了一个这样的帮助功能:
const char* name(Id id) {
struct Entry {
Id id;
const char* name;
};
static const Entry entries[] = {
{ ErrorA, "ErrorA" },
{ ErrorB, "ErrorB" },
{ 0, 0 }
}
for (int it = 0; it < gui::SiCount; ++it) {
if (entries[it].id == id) {
return entries[it].name;
}
}
return 0;
}
Linear search is usually more efficient than std::map
for small collections like this.
线性搜索通常比这样std::map
的小集合更有效。