Article View: borland.public.cppbuilder.nativeapi
Article #10275Re: What can I use instead of go through a List?
From: PC Hua
Date: Wed, 12 May 2004 04:06
Date: Wed, 12 May 2004 04:06
92 lines
3300 bytes
3300 bytes
Does it work like this as well:
ItemIter finder = items.find( 123456);
to retrieve the name?
Can I also use it for complex struct?
If you can't antwer that from the head, then leave it. I will try to look
up in the online help tonight.
Thanks for your time and the detailed exsample.
Phuoc Can HUA
On Tue, 11 May 2004 20:52:42 -0400, Ed Mulroy [TeamB]
<dont_email_me@bitbuc.ket> wrote:
> 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?
>
>
--
_/\__/\__/\__/\__/\__/\_
www.folksfun.com
Technical Details
Message-ID:
<opr7vcctm1pmxkmr@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<opr7uyeayypmxkmr@newsgroups.borland.com> <40a17569@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 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: What can I use instead of go through a List? References: <opr7uyeayypmxkmr@newsgroups.borland.com> <40a17569@newsgroups.borland.com> Message-ID: <opr7vcctm1pmxkmr@newsgroups.borland.com> From: PC Hua <home-AT-huaonline.DOT.com> Content-Type: text/plain; format=flowed; charset=iso-8859-15 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Wed, 12 May 2004 14:06:19 +1000 User-Agent: Opera7.21/Win32 M2 build 3218 NNTP-Posting-Host: 203.194.21.162 X-Trace: newsgroups.borland.com 1084334780 203.194.21.162 (11 May 2004 21:06:20 -0700) Lines: 92 Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail