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?
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";
User contributions licensed under CC BY-SA 3.0