Lets say I have the code
Code:
enum sKind_t { S_BLUE, S_GREEN, S_YELLOW };
and
Code:
struct square_t { sKind_t kind; string name; };
And Had a file Like this
BLUE apple
GREEN square
GREEN apple
YELLOW Horse
You then write some code to split up the file into two seperrate strings for each line
example
string A= BLUE
string B = apple
how do you store the enum value via a string
so i want say
square_t.kind = A;
but this doesn't work
how do you get strings that are one of the correct values for an enum to work as an enum category?
you change string A to "S_" + A giving S_BLUE
In C++ how do you convert a string to enum?
As the other answerer has said you cannot directly convert from string to enum. What you can do is have a mapping between the strings and the enum. One simple way would be comparing the inputted string with a colour like BLUE, YELLOW, GREEN. If the string is one of these, then use the enum value of it, like S_BLUE, S_YELLOW, S_GREEN.
I do not understand what you are trying to do, but this may help.
Reply:Enum types are named integer constants. You cannot directly convert them to C-style strings, or vice versa.
Reply:I think you need a dictionary. You can use the STL %26lt;map%26gt; class which operates in logarithmic time or for constant time you can use a hash table.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment