Alternative for String Comparision in C++
Posted on 2002-04-10
I have defined more than 100 strings like,
TCHAR * MyString1 = _T("String1");
TCHAR * MyString2 = _T("String2");
TCHAR * MyString200 = _T("String200");
I have a function, ReadAndAssign(TCHAR * szString)
The function will check if the szString is any of MyString1, MyString2...MyString200, if so, it will do some operation.
The function is defined as below.
void ReadAndAssign(TCHAR *szString)
if(wcscmp(szString,MyString1) == 0)
//call func 1
if(wcscmp(szString,MyString2) == 0)
//call func 2
if(wcscmp(szString,MyString200) == 0)
//call func 200
The above definition has the worst case scenario if the string being searched appears at the end of the function.
The program counter has to gone thru all the statements. Also, VC++ compiler doesn't allow more than 128 levels of if--else.
Can you suggest a better way of doing this???????