Feb 25, 2015 at 9:54am Feb 25, 2015 at 9:54am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <iostream>
#include <map>
enum difficulty {Easy, Normal, Hard};
const char * diff_names[3] = {"Easy" , "Normal" , "Hard" }; //1
const std::map<difficulty, const char *> difficulty_name = { //2
{Easy, "Easy" }, {Normal, "Normal" }, {Hard, "Hard" },
};
//3 Not the smartest idea. Bad.
#define _tostr(X) (#X)
#define tostr(X) _tostr(X)
int main()
{
std::cout << diff_names[Normal] << '\n' ;
std::cout << difficulty_name.at(Easy) << '\n' ;
std::cout << tostr(Hard);
}
Last edited on Feb 25, 2015 at 10:06am Feb 25, 2015 at 10:06am UTC
Feb 25, 2015 at 10:00am Feb 25, 2015 at 10:00am UTC
I'm not familiar with map, it looks to me like some sort of casting?
Feb 25, 2015 at 10:07am Feb 25, 2015 at 10:07am UTC
No, it similar to array, but its keys are not integers, but some other type.
Feb 25, 2015 at 10:10am Feb 25, 2015 at 10:10am UTC
There is no automatic way to get the name of the enums. You will have to write the code to convert the enum/int to a string yourself. MiiNiPaa has shown you two different ways of doing it (ignore the third one). If you don't know maps you can probably at least grasp the array method. If you want you can use std::string instead of const char*.
const std::string diff_names[3] = {"Easy" , "Normal" , "Hard" };
Last edited on Feb 25, 2015 at 10:10am Feb 25, 2015 at 10:10am UTC
Feb 25, 2015 at 11:20am Feb 25, 2015 at 11:20am UTC
yea the array method I can get, need to read up on maps imtereted in them now :D