How to iterator the enum in c++ like java?

0

Possible Duplicate:
C++: Iterate through an enum

I have following enum in c++;

typedef enum
{
    APP_NONE = 0,
    APP_SESSION = 0x80000001,
    APP_TITLE = 2,
} APP_TYPE;

I am writing a test function, which accept a string, get related integer, such as: getEnumByString("APP_NONE") = 0; or vice verse getEnumString(0)="APP_NONE".

Is it possible and how to finish it?

c++
enums
asked on Stack Overflow Jul 3, 2012 by David Guo • edited May 23, 2017 by Community

2 Answers

1

You can push the enum values into a container, like a vector or a set, and iterate through that.

std::vector<APP_TYPE> types;
types.push_back(APP_NONE);
types.push_back(APP_SESSION);
types.push_back(APP_TITLE);

You can use a map to associate the enum values with a string and vice versa.

std::map<APP_TYPE, std::string> type2string;
type2string[APP_NONE] = "APP_NONE";
type2string[APP_SESSION] = "APP_SESSION";
type2string[APP_TITLE] = "APP_TITLE";
answered on Stack Overflow Jul 3, 2012 by jxh • edited Jul 3, 2012 by jxh
0

User contributions licensed under CC BY-SA 3.0