Solved

Alternative for String Comparision in C++

Posted on 2002-04-10
10
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

740 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