Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Map to stor...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 8 Topic 45803 of 47031
Post > Topic >>

Re: Map to store 3 items

by "Alf P. Steinbach" <alfps@[EMAIL PROTECTED] > May 8, 2008 at 04:02 AM

* Brad:
> New to C++. Just ran into a need to store three things in a map. Before 
> trying it, I thought I'd post here and ask for advice. Here is an 
> example of what I need to store:
> 
> map<string name, string prefix, int length>
> 
> Both name and prefix would be unique for each map entry. I was wondering

> if the the first entry could be an array that holds the two items (name 
> and prefix), but then I wondered how an iterator would behave when 
> it->first is called.
> 
> Any suggestions or pointers on using a map with three items would be 
> much appreciated.

A map is an association from key to (key, value).  Abstractly it's just an

association from key to value.  You need to decide what your key is, and
what 
your value is.  The value can be anything copyable, such as a struct with
two 
members.  E.g. you can have a map< string, pair<string, int> >.

Before doing that, however, it might be an idea to think through what the 
problem you're trying to solve is, not how to use a map to solve it.

For example, is the 'length' the number of characters in name or prefix? 
If so, 
then you already have it in the name or prefix string.  Or is the length
perhaps 
the sum of the name and prefix lengths?  In that case it's easier to
compute it 
than look it up in a map.  Some analysis is always good. :-)


Cheers, & hth.,

- Alf

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 




 8 Posts in Topic:
Map to store 3 items
Brad <brad@[EMAIL PROT  2008-05-07 21:26:03 
Re: Map to store 3 items
"Alf P. Steinbach&qu  2008-05-08 04:02:24 
Re: Map to store 3 items
Jerry Coffin <jcoffin@  2008-05-07 22:36:57 
Re: Map to store 3 items
Juha Nieminen <nospam@  2008-05-08 10:44:14 
Re: Map to store 3 items
Jerry Coffin <jcoffin@  2008-05-08 19:07:21 
Re: Map to store 3 items
Paavo Helde <nobody@[E  2008-05-08 00:03:55 
Re: Map to store 3 items
James Kanze <james.kan  2008-05-09 05:37:52 
Re: Map to store 3 items
=?utf-8?Q?David_C=C3=B4me  2008-05-08 10:34:26 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 15:46:55 CDT 2008.