Solved

Search String table

Posted on 2014-02-25
6
270 Views
Last Modified: 2014-03-01
Hello,

i have a list of thousand of different IDs. A sample:
static struct
{
  TCHAR org_openbddb[41];
  TCHAR cross_openbddb[41];
} openbddb_matrix[] = {
/* The ISO 639 language codes.
 */
	{_T("ec62b87bfd5585ac8be5c7fb0cbba1ce6a29c0bd"), _T("ec62b87bfd5585ac8be5c7fb0cbba1ce6a29c0bd")}, 


};

Open in new window


I need to search the value "org_openbddb" in the list and return the "cross_openbddb" value if found. If nothing found, return "". Any ide how to do this?
0
Comment
Question by:Ingo Foerster
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39887435
in c++, you can use std::map
0
 
LVL 86

Expert Comment

by:jkr
ID: 39887447
If you need a quick lookup that you have to perform often, I'd suggest storing these structs in a 'std::map' to look them up.

#include <map>
using namespace std;

static struct
{
  TCHAR org_openbddb[41];
  TCHAR cross_openbddb[41];
} openbddb_matrix[] = {
/* The ISO 639 language codes.
 */
	{_T("ec62b87bfd5585ac8be5c7fb0cbba1ce6a29c0bd"), _T("ec62b87bfd5585ac8be5c7fb0cbba1ce6a29c0bd")}, 

{NULL, NULL} // I assume this is at the end
};
 
// ...
typedef std::string<TCHAR> tstring;

map<tstring,tstring> map_lang_code;

// store all in map
int n = 0;
while(openbddb_matrix[n].org_openbddb) {

  map_lang.code.insert(map<tstring,tstring>::value_type(openbddb_matrix[i].org_openbddb,openbddb_matrix[i].cross_openbddb));
  ++n;
}

TCHAR* lookup_org_value(const TCHAR* val) {

  // lookup
  map<tstring,tstring>::iterator i;

// e.g. like: i = map_lang_code.find(_T("ec62b87bfd5585ac8be5c7fb0cbba1ce6a29c0bd"));

  i = map_lang_code.find(val);

  if (i != map_lang_code.end()) return i->second; // found it

  return NULL;  // nothing found
}

Open in new window

0
 
LVL 86

Accepted Solution

by:
jkr earned 345 total points
ID: 39887460
On the other hand, if you only need to do that once or twice, a simple loop will do:

TCHAR* lookup_org_value(const TCHAR* val) {

  int n = 0;
  while(openbddb_matrix[n].org_openbddb) {

     if (!_tcscmp(openbddb_matrix[n].org_openbddb,val)) return openbddb_matrix[n].cross_openbddb; // found it
     ++n;
  }

  return NULL; // nothing found
}

Open in new window

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 28

Expert Comment

by:pepr
ID: 39888100
If you use C++11 or if you can use Boost, the unordered_map can be faster (better in O() terms). However, it probably should be measured if it matters. (As you probably use Visual Studio, the older versions named the unordered_map as hash_map.)
0
 
LVL 33

Expert Comment

by:sarabande
ID: 39888430
ec62b87bfd5585ac8be5c7fb0cbba1ce6a29c0bd

Open in new window

the codes seem to be 20 unsigned 8-bit integers (bytes) in hex notation. if so, you could spare more than half the size if you convert them to binary integers. if your TCHAR is a wide character the saving would be more than 3/4 of the size needed for strings.

to use a std::map with the 20 bytes you probably would define a little class like that:

class LangCode
{
     unsigned char codes[20];
public:
     LangCode() { memset(codes, 0, sizeof(codes)); }
     LangCode(TCHAR scod[]) 
     { 
        assert(_tcslen(scod) == 40); 
        char szt[3] = { '\0' };
        for (int i = 0, j = 0; i < 40; i += 2, j++)
        {
             szt[0] = (char)scod[i];
             szt[1] = (char)scod[i+1];
             std::istringstream iss(szt);
             iss >> std::hex >> codes[j];
             assert(iss);
        }
     }
 
     bool operator<(const LangCode& lc) const
     { return memcmp(codes, lc.codes, sizeof(codes)) < 0; } 
};

Open in new window

                               
with that you could add and find members like

    TCHAR * psz1 = L"ec62b87bfd5585ac8be5c7fb0cbba1ce6a29c0bd";
    TCHAR * psz2 = L"23b87bfd5585ac8fffc7fb0cba1ba1ce6a200000";
    std::map<LangCode, LangCode> maplc;

    maplc[psz1] = psz2;
    ...
    std::map<LangCode, LangCode>::iterator f;
    if ((f = maplc.find(psz1)) != maplc.end())
    {
        std::cout << "found" << std::endl;
    }
    else
    {
        std::cout << "not found" << std::endl;
    }

Open in new window


you see the map would make an implicit conversion when using TCHAR strings.

if you read the codes from a database you would fill the map when loading the data.

if you have all the mapping code pairs in a 'matrix' like you posted you could do

struct LCPair
{
    LangCode lc1;
    LangCode lc2;
};

LCPair matrix[] = 
{ 
{_T("ec62b87bfd5585ac8be5c7fb0cbba1ce6a29c0bd"),
    _T("23b87bfd5585ac8fffc7fb0cba1ba1ce6a200000") }, 
{_T("ff62b87bfd5325ac8be5c7fb0cbba1ce6a29c0bd"),
    _T("11111afd5585ac8fffc7fb0cba1ba1ce6a200000")}, 
....
};

Open in new window


after that you could fill the map by code like

for (size_t n = 0; n < sizeof(matrix)/sizeof(matrix[0]); ++n)
     maplc[matrix[n].lc1] = matrix[n].lc2;

Open in new window


alternatively, you could sort the matrix by the first member and then make a binary search on the sorted matrix instead of using a map.

Sara
0
 

Author Closing Comment

by:Ingo Foerster
ID: 39897851
Great. Thank you.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
valid enum? 6 74
Eclipse IDE - Cannot copy/paste from console output 8 183
How to install SVN Command Line Client? 5 65
delete-remove 14 84
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

816 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

10 Experts available now in Live!

Get 1:1 Help Now