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

I

Ian Collins

Ramon said:
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.
 
R

Ramon F Herrera

[I'm reading this in comp.programming]

In <[email protected]>,
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.org.uk>
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
 
J

Juha Nieminen

Ramon said:
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.)
 
D

Dave Searles

Ramon said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top