Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Alternative for String Comparision in C++

Posted on 2002-04-10
10
Medium Priority
?
399 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

722 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