Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::map associative container question

Reply
Thread Tools

std::map associative container question

 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-22-2006
Diego Martins wrote:

>
> Kai-Uwe Bux wrote:
>> aaragon wrote:
>>
>> > Hello everyone,
>> >
>> > I would like to know if there is a way to use the std::map to store
>> > different types for one of its two types. That is, I'm trying to use
>> > it as:
>> >
>> > typedef std::map<string,double> Map;
>> >
>> > but instead of double, I have integers, even booleans so I guess that
>> > the use of double waste a lot of storage. Is there any way to do this?

>>
>> The short answer is: no. The long answer is: yes, but the overhead
>> incurred be the logic needed to arrange for one object to represent
>> values from possible different types is very likely going to null and
>> void all possible gains in storage consumption.
>>
>> E.g., you could do something like:
>>
>> struct number {
>> ~number();
>> };

>
> virtual ~number(); , instead
>
>> struct number_double : public base {
>> double value;
>> };
>>
>> struct number_int : public base {
>> int value;
>> };

>
> public number, instead of public base
>
>> ...
>>
>> std::map<string,number*> Map;


Oops, thanks for the corrections.

> Kai, don't get me wrong.


Let me make one of my own: it's "Kai-Uwe".

> Actually, I know it was only typos, but my intention is help the OP.


That's always understood. To tell the truth, "base" was a typo. The
missing "virtual" was a real misthink: I had the virtual in there at some
point and deleted it thinking I could do without. Don't ask why or what I
was thinking -- it's beyond me now, especially since I went on to write
that number has to be polymorphic.


Thanks again

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
Diego Martins
Guest
Posts: n/a
 
      10-17-2006

Victor Bazarov wrote:
> Diego Martins wrote:
> > Kai-Uwe Bux wrote:
> >> aaragon wrote:
> >>
> >>> Hello everyone,
> >>>
> >> ...

> >
> > Kai, don't get me wrong. Actually, I know it was only typos, but my
> > intention is help the OP.

>
> The name you should have used is "Kai-Uwe", not "Kai". Don't get me
> wrong, Diego.


Sorry about that.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An associative container jacob navia C Programming 21 11-21-2009 03:44 PM
Why "associative" in associative container? desktop C++ 5 06-26-2007 07:49 AM
Sortable associative container? Matthias =?ISO-8859-1?Q?K=E4ppler?= C++ 7 12-03-2004 01:12 AM
associative container T.Meitz C++ 2 02-17-2004 06:13 PM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments