Article View: borland.public.cppbuilder.nativeapi
Article #10277Re: What can I use instead of go through a List?
From: Ed Mulroy [TeamB]
Date: Wed, 12 May 2004 05:22
Date: Wed, 12 May 2004 05:22
63 lines
3323 bytes
3323 bytes
> Does it work like this as well:
> ItemIter finder = items.find( 123456);
There are two items, the key and the data. With a map what one
normally searches for is the data and in this case 123456 is the data.
Yes, you can search for the key but that isn't the normal thing with
this type of element and you don't do it in exactly the same way.
for (finder = items.begin(); finder != items.end(); ++finder)
if (finder->second == 123456)
ShowMessage(finder->first);
(or, for a real program, write a little KeyFind function that does
this)
If what you are doing is looking for the data most of the time, which
is what your question said, then map was written to address that
situation. It simulates associative memory (content addressable
memory) where the key content is the element's address. Internally is
a tree arranged so that finding the key takes minimal processing.
When one searches for the data to find the key he is using it similar
to how a vector or one of the other container classes are used. While
that is not 'bad' or 'wrong', it doesn't gain special benefit from the
configuration of a map container in the manner that a search for a key
does.
One thing I like a lot about a map is that when you use the
square-bracket, array style of addressing then if the key doesn't
exist it creates one. For example:
typedef map<unsigned, double> YearlyEarningsType;
YearlyEarningsType earnings;
:
earnings[1999] = 40000.00;
earnings[2000] = 48000.00;
earnings[2001] = 53000.00;
earnings[2002] = 56000.00;
earnings[2004] = 61000.00;
Unlike an array you don't need waste memory on earnings[0] through
earnings[1998] to have earnings[1999]. (and no, those numbers aren't
what I earned in those years)
> Can I also use it for complex struct?
Yes. Make the complex struct the data type.
map<the_key_type, the_data_type>
. Ed
> PC Hua wrote in message
> news:opr7vcctm1pmxkmr@newsgroups.borland.com...
>
> 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.
Technical Details
Message-ID:
<40a1b4c1@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> <opr7vcctm1pmxkmr@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> <40a17569@newsgroups.borland.com> <opr7vcctm1pmxkmr@newsgroups.borland.com> Subject: Re: What can I use instead of go through a List? Date: Wed, 12 May 2004 01:22:56 -0400 Lines: 65 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: <40a1b4c1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1084339393 24.74.175.102 (11 May 2004 22:23:13 -0700) X-Authenticated-User: edmulroy 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