[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Is there a type of map which preserves the order of insertion?

Ramon F Herrera

9/20/2009 3:14:00 AM


Is there a type of map which preserves the order of insertion?

-Ramon

8 Answers

Ramon F Herrera

9/20/2009 4:01:00 AM

0

On Sep 19, 11:14 pm, Ramon F Herrera <ra...@conexus.net> wrote:
> Is there a type of map which preserves the order of insertion?
>
> -Ramon

The question is about C++.

-RFH

Richard Heathfield

9/20/2009 4:38:00 AM

0

[I'm reading this in comp.programming]

In <c10b023a-bf7b-4067-8fd9-39751fed66ac@v2g2000vbb.googlegroups.com>,
Ramon F Herrera wrote:

> Is there a type of map which preserves the order of insertion?

Why not just wrap up a vector and a map in your own class? Vector of
keys (indexed on insertion order), and perhaps keep a note of the
insertion order in the payload for the map, so that you can shift
back to the vector when required?

--
Richard Heathfield <http://www.cpax....
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

Ian Collins

9/20/2009 4:52:00 AM

0

Ramon F Herrera wrote:
> Is there a type of map which preserves the order of insertion?

I assume by the cross posting (why do you keep doing that? maybe you
should try alt.comp.lang.learn.c-c++) you are asking about C++?

If so, then no, you have to roll your own as Richard suggested.

--
Ian Collins

Ramon F Herrera

9/20/2009 5:04:00 AM

0

On Sep 20, 12:37 am, Richard Heathfield <r...@see.sig.invalid> wrote:
> [I'm reading this in comp.programming]
>
> In <c10b023a-bf7b-4067-8fd9-39751fed6...@v2g2000vbb.googlegroups.com>,
>
> Ramon F Herrera wrote:
> > Is there a type of map which preserves the order of insertion?
>
> Why not just wrap up a vector and a map in your own class? Vector of
> keys (indexed on insertion order), and perhaps keep a note of the
> insertion order in the payload for the map, so that you can shift
> back to the vector when required?
>
> --
> Richard Heathfield <http://www.cpax....
> Email: -http://www. +rjh@
> "Usenet is a strange place" - dmr 29 July 1999
> Sig line vacant - apply within

Thanks, Richard.

That was exactly my idea (the first of your two suggestions), but
wanted to check first, before trying to roll my own.

Thanks,

-Ramon

Juha Nieminen

9/20/2009 8:02:00 AM

0

Ramon F Herrera wrote:
> Is there a type of map which preserves the order of insertion?

Yes, it's called a vector. Although a list could do too.

(Oh, you wanted O(log n)-time operations? Now that's an algorithmical
problem which has no one single unambiguous solution.)

Dave Searles

9/20/2009 7:41:00 PM

0

Ramon F Herrera wrote:
> Is there a type of map which preserves the order of insertion?

Java has a LinkedHashMap class that does this.

In any other language you can use map whose value type is a linked list
node rather than directly the value objects, wrap it in a class (or
whatever), and make your own LinkedHashMap-alike.

Patricia Shanahan

9/20/2009 9:59:00 PM

0

Ramon F Herrera wrote:
> On Sep 20, 12:37 am, Richard Heathfield <r...@see.sig.invalid> wrote:
>> [I'm reading this in comp.programming]
>>
>> In <c10b023a-bf7b-4067-8fd9-39751fed6...@v2g2000vbb.googlegroups.com>,
>>
>> Ramon F Herrera wrote:
>>> Is there a type of map which preserves the order of insertion?
>> Why not just wrap up a vector and a map in your own class? Vector of
>> keys (indexed on insertion order), and perhaps keep a note of the
>> insertion order in the payload for the map, so that you can shift
>> back to the vector when required?
>>
>> --
>> Richard Heathfield <http://www.cpax....
>> Email: -http://www. +rjh@
>> "Usenet is a strange place" - dmr 29 July 1999
>> Sig line vacant - apply within
>
> Thanks, Richard.
>
> That was exactly my idea (the first of your two suggestions), but
> wanted to check first, before trying to roll my own.

If deletion speed matters, consider using a doubly linked list, where
either the linked list element is the map payload or the map payload
contains a pointer to it.

Patricia

Alf P. Steinbach

9/21/2009 1:49:00 AM

0

* Patricia Shanahan:
> Ramon F Herrera wrote:
>> On Sep 20, 12:37 am, Richard Heathfield <r...@see.sig.invalid> wrote:
>>> [I'm reading this in comp.programming]
>>>
>>> In <c10b023a-bf7b-4067-8fd9-39751fed6...@v2g2000vbb.googlegroups.com>,
>>> Ramon F Herrera wrote:
>>>> Is there a type of map which preserves the order of insertion?
>>> Why not just wrap up a vector and a map in your own class? Vector of
>>> keys (indexed on insertion order), and perhaps keep a note of the
>>> insertion order in the payload for the map, so that you can shift
>>> back to the vector when required?
>>
>> That was exactly my idea (the first of your two suggestions), but
>> wanted to check first, before trying to roll my own.
>
> If deletion speed matters, consider using a doubly linked list, where
> either the linked list element is the map payload or the map payload
> contains a pointer to it.

I think the problem is underspecified, thus any particular implementation choice
perhaps a bit premature.

For example, what should

MySequentialMap<int, char const*> map;

map[1] = "One";
map[2] = "Two";
map[3] = "Three";
map[1] = "Uno";
for( Iter it = map.begin(); it != map.end(); ++it )
{
cout << it->second << endl;
}

produce?

How about if, between the last two assignments, the original [1] entry is
deleted? Does it even make sense to talk about deletion here?


Cheers,

- Alf