🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #10271

Re: What can I use instead of go through a List?

#10271
From: Ed Mulroy [TeamB]
Date: Wed, 12 May 2004 00:52
69 lines
2750 bytes
There is a kind of list that is in C++ that automatically is sorted.
It's called a map.  For example, when you put things into it you can
index it on the string and get out the number associated with the
string.  The string would be the key.

Try running this:

  -------------------
  #include <vcl.h>
  #pragma hdrstop
  #include <map>
  using namespace std;

  struct MyCompare // so it knows how to compare the keys
    {
    bool operator()(const AnsiString& a1, const AnsiString& a2)
      {
      return a1 < a2;
      }
    };

  // map with an AnsiString for a key, unsigned for data
  typedef map<AnsiString, unsigned, MyCompare> ItemMap;
  typedef ItemMap::iterator ItemIter;

  WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
    {
    Application->Initialize();
    ItemMap items;

    items["Grape"] = 78901;
    items["Bananna"] = 123456;
    items["Apple"] = 56789;
    items["Strawberry"] = 32768;

    AnsiString result;
    ItemIter finder = items.find("Bananna");

    result.printf("Item: %s ID: %d", finder->first, finder->second);
    ShowMessage(result);

    for (finder = items.begin(); finder != items.end(); ++finder)
      {
      result.sprintf("Item: %s ID: %d", finder->first,
finder->second);
      ShowMessage(result);
      }

    return 0;
    }

  -------------------

.  Ed

> PC Hua wrote in message
> news:opr7uyeayypmxkmr@newsgroups.borland.com...
>
> For instance, I have a list (the length is known) of
> components:
>    Item.Name="Apple";
>    Item.ID= 56789; // Sytem generated and initialized in
> runtime
>    ...
> Now my problem is to retrieve the name for a given ID.
> Is there any implementation of a list which allows to find the
> name of the
> item efficiently?
> Any suggestion?
Technical Details
Message-ID: <40a17569@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <opr7uyeayypmxkmr@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,9143cde94826dba9
X-Google-Attributes: gid10cc67,public
From: "Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <opr7uyeayypmxkmr@newsgroups.borland.com>
Subject: Re: What can I use instead of go through a List?
Date: Tue, 11 May 2004 20:52:42 -0400
Lines: 71
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 24.74.175.102
Message-ID: <40a17569@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1084323177 24.74.175.102 (11 May 2004 17:52:57 -0700)
X-Authenticated-User: edmulroy
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail