Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

Alternative for String Comparision in C++

Hi,
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???????

Thanks,
Baidehish
0
bdwibedy
Asked:
bdwibedy
  • 6
  • 3
1 Solution
 
AxterCommented:
Try using std::set to store the strings, and then check if your new string exist in your std::set.
0
 
jkrCommented:
What about using a STL map? E.g.

#include <map>
#include <string>

typedef basic_string<TCHAR> tstring;

typedef void (*FUNC_TO_CALL)(); // function pointer

map<tstring,FUNC_TO_CALL> map_str_to_func;

FUNC_TO_CALL f; // initialize

// fill map
map_str_to_func.insert ( map<tstring,FUNC_TO_CALL>::value_type ( "String1", f));


// lookup

map<tstring,FUNC_TO_CALL>::iterator i;

i = map_str_to_func.find ( _T("String1));

if ( map_str_to_func.end != i) {

 f = (*i).second;
}
0
 
AxterCommented:
Here's a quick example using std::set

#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <set>

using namespace std;

std::set<std::string> MyStringList;

char * MyString200 = "String200";


void ReadAndAssign(char *szString)
{
     if (MyStringList.find(szString) == MyStringList.end())
     {
          strcpy(szString, MyString200);
     }
}


int main(int argc, char* argv[])
{
     MyStringList.insert("String1");
     MyStringList.insert("String6");
     MyStringList.insert("String2");

     char test1[32] = "bla bla bal";
     char test2[32] = "String2";
     ReadAndAssign(test1);
     ReadAndAssign(test2);

     std::cout << "test1 = "  << test1 << " test2 = " << test2 << std::endl;

     system("pause");
     return 0;
}

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AxterCommented:
This example uses TCHAR

#include <iostream>
#include <string>
#include <set>

using namespace std;

typedef std::basic_string<TCHAR> TCHAR_STR;

std::set<TCHAR_STR> MyStringList;

TCHAR * MyString200 = _T("String200");


void ReadAndAssign(TCHAR *szString)
{
     if (MyStringList.find(szString) == MyStringList.end())
     {
          _tcscpy(szString, MyString200);
     }
}


int main(int argc, char* argv[])
{
     MyStringList.insert(_T("String1"));
     MyStringList.insert(_T("String6"));
     MyStringList.insert(_T("String2"));

     TCHAR test1[32] = _T("bla bla bal");
     TCHAR test2[32] = _T("String2");
     ReadAndAssign(test1);
     ReadAndAssign(test2);

     if (sizeof(TCHAR) == 1)
     {
          std::cout << _T("test1 = ")  << test1 << _T(" test2 = ") << test2 << std::endl;
     }
     else
     {
          std::wcout << _T("test1 = ")  << test1 << _T(" test2 = ") << test2 << std::endl;
     }

     system("pause");
     return 0;
}

0
 
bdwibedyAuthor Commented:
Unfortunately, I can't use the STL in my project. The product is a real time embedded system product, so I try to minimize the usage libraries.

Any other option pls...using clean C++?

Thanks,
Baidehish
0
 
jkrCommented:
What about a simple array?

typedef void (*FUNC_TO_CALL)(); // function pointer

struct STRING_MAPPER {
 
TCHAR* psz;
FUNC_TO_CALL f;
};

STRING_MAPPER aMapper [ MAPPER_MAX] =
{
{ _T("String1"), f1},

//...

{ _T("StringN"), fN},
{ NULL, NULL}
};

void ReadAndAssign(TCHAR *szString) {

 for ( int i = 0; aMapper [ i].psz; i++) {

   if ( wcscmp ( szString, aMapper[ i].psz)) {
        (aMapper [ i].f) ();
   }
 }
}
0
 
AxterCommented:
You could try putting it in a sorted array, and then use bsearch function to determine if there's a match.
0
 
AxterCommented:
The key to speeding up your code, is to do a search to a sorted array.
std:set would have done this for you automatically, but you can do something similar by using qsort and bsearch functions.
0
 
jkrCommented:
>>The key to speeding up your code, is to do a search to a
>>sorted array.

That's why I suggested using an array. From that point on, a lot of optimization coud be done. Even hashing would be an option...
0
 
AxterCommented:
Here's an example using bsearch and qsort.

const int NUM_ELM = 4;
const int MAX_SIZE_ITEM = 32;
TCHAR MyStringList[NUM_ELM][MAX_SIZE_ITEM] = {_T("String4"),_T("String6"),_T("String1"),_T("String2")};

TCHAR MyString200[MAX_SIZE_ITEM] = _T("String200");

int compare( const void *arg1, const void *arg2 )
{
   /* Compare all of both strings: */
   return strcmp( (char*)arg1, (char*)arg2 );
}


void ReadAndAssign(TCHAR *szString)
{
     TCHAR* ptr = (TCHAR*)bsearch(szString, MyStringList, NUM_ELM, MAX_SIZE_ITEM, compare );
     if (!ptr)
     {
          _tcscpy(szString, MyString200);
     }
}


int main(int argc, char* argv[])
{

     qsort(MyStringList, NUM_ELM, MAX_SIZE_ITEM, compare);
     TCHAR test1[MAX_SIZE_ITEM] = _T("bla bla bal");
     TCHAR test2[MAX_SIZE_ITEM] = _T("String4");
     ReadAndAssign(test1);
     ReadAndAssign(test2);

     if (sizeof(TCHAR) == 1)
     {
          std::cout << _T("test1 = ")  << test1 << _T(" test2 = ") << test2 << std::endl;
     }
     else
     {
          std::wcout << _T("test1 = ")  << test1 << _T(" test2 = ") << test2 << std::endl;
     }

     system("pause");
     return 0;
}

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now