Solved

Alternative for String Comparision in C++

Posted on 2002-04-10
10
385 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

708 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

19 Experts available now in Live!

Get 1:1 Help Now