Solved

Alternative for String Comparision in C++

Posted on 2002-04-10
10
386 Views
Last Modified: 2013-12-14
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
Comment
Question by:bdwibedy
  • 6
  • 3
10 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 6931970
Try using std::set to store the strings, and then check if your new string exist in your std::set.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6931973
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
 
LVL 30

Expert Comment

by:Axter
ID: 6932020
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
 
LVL 30

Expert Comment

by:Axter
ID: 6932078
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
 

Author Comment

by:bdwibedy
ID: 6932111
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6932136
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
 
LVL 30

Expert Comment

by:Axter
ID: 6932167
You could try putting it in a sorted array, and then use bsearch function to determine if there's a match.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6932172
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
 
LVL 86

Expert Comment

by:jkr
ID: 6932186
>>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
 
LVL 30

Expert Comment

by:Axter
ID: 6932199
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now