Skip to main content
gamedev.net
🔒 Locked

char * to enum

Started by siliconsurfer Sep 16, 2005 at 5:49 AM 6 replies 4.3k views
Original Post
siliconsurfer
siliconsurfer
Hi, quick question. I have a enum type called stage. So I can have write:- stage theStage = first; stage theStage = second; etc etc I was wondering is there any way, if I read in the string "first" from a text file and store it in char *stageString, can I somehow convert the char * to the enum type. i.e stage theStage = stageString; Cheers
ZQJ
ZQJ
I'm pretty sure the answer is no. You'll have to compare the string against each possibility and assign it yourself.
Sharlin
Sharlin
Well, no need to compare against all of them, just do a binary search (O(lg n)) or a hash lookup (O(1) average case) in an array of (string, enum) mappings. The easiest way is probably to go with std::map.
Enigma
Enigma
C++:
enum MyEnum{	EnumValue1 = value1,	EnumValue2 = value2};typedef std::map< std::string, MyEnum > StringToEnumTable;typedef StringToEnumTable::const_iterator StringToEnumLookup;StringToEnumTable buildStringToEnumTable(){	StringToEnumTable table;	table.insert(std::make_pair("EnumValue1", EnumValue1));	table.insert(std::make_pair("EnumValue2", EnumValue2));	return table;}StringToEnumTable stringToEnum = buildStringToEnumTable();void someFunction(){	std::ifstream reader("myDataFile.dat");	std::string enumName;	reader >> enumName;	StringToEnumLookup enumValue = stringToEnum.find(enumName);	if (enumValue != stringToEnum.end())	{		doSomething(enumValue->second);	}	else	{		throw EnumValueNotFound(enumName);	}}


moeron seems to have deleted his post while I was replying
Quote:
Original post by moeron
if( !stricmp(szStageOne, currentToken.c_str() ) )

There is a special circle of hell reserved for those who needlessly use C string functions in C++.

Enigma
Kippesoep
Kippesoep
Quote:
Original post by Enigma
Quote:
Original post by moeron
if( !stricmp(szStageOne, currentToken.c_str() ) )

There is a special circle of hell reserved for those who needlessly use C string functions in C++.

If only C++ strings actually contained an easy method to actually do this (that is: case-insensitive comparison). It's not quite as needless as it may seem.
Kippesoep
moeron
moeron
Quote:
Original post by Enigma
C++:
*** Source Snippet Removed ***

moeron seems to have deleted his post while I was replying
Quote:
Original post by moeron
if( !stricmp(szStageOne, currentToken.c_str() ) )

There is a special circle of hell reserved for those who needlessly use C string functions in C++.

Enigma


Yeah, I just woke up when I responded. After I read the other poster's map response I realized that may method wasn't of much help

moe.ron

[EDIT]
But even using the map method there are C-String comparisons done on the std::string if you use them as a key-type...
moe.ron
Puzzler183
Puzzler183
Quote:
Original post by Sharlin
Well, no need to compare against all of them, just do a binary search (O(lg n)) or a hash lookup (O(1) average case) in an array of (string, enum) mappings. The easiest way is probably to go with std::map.


O(1) if you ignore the cost of hashing.
flangazor
flangazor
Quote:
Original post by Puzzler183
Quote:
Original post by Sharlin
Well, no need to compare against all of them, just do a binary search (O(lg n)) or a hash lookup (O(1) average case) in an array of (string, enum) mappings. The easiest way is probably to go with std::map.


O(1) if you ignore the cost of hashing.

That's what the O in O(1) is.

I've had the same problem intent as the OP before and used Enigma's solution. However, I make a const array of pairs and build the map with iterators.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.