Archived

This topic is now archived and is closed to further replies.

Toolmaker

Preventing a map from being sorted

Recommended Posts

Toolmaker    967
I am currently using a map to access the items by name. But, I don''t want all the items to be sorted since they will be written to file and I like it to be in the same order as when I added them to the map. How can I prevent this? Toolmaker
-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
felonius    122
You can''t. The whole point of the map datastructure is to garantee fast lookup times (in O(logn) time) and to do this the elements would have to be sorted by their key.

But what you need to do is to consider if map is what you really need. What about making a list of pairs of key-value pairs. If you push_back on that list then ordering is garanteed to remain constant.



Jacob Marner, M.Sc.
Console Programmer, Deadline Games

Share this post


Link to post
Share on other sites
Toolmaker    967
hmm, I want to items not to be sorted because the file would look better in notepad when I open it. Anyway, I don''t need to manual edit the files(maybe 1 or 2 exceptional occassions) so I just keep it this way.

Toolmaker



-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
Shannon Barber    1681
Maintain a list(or vector) & a map. The list keeps everything in order, the map allows fast look-ups.

- Magmai Kai Holmlor

"No, his mind is not for rent to any god nor government" - Rush, Tom Sawyer

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
[Free C Libraries | zlib ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
there are a number of different implementations of the STL

Share this post


Link to post
Share on other sites
Toolmaker    967
Good idea Magmai. I am going to implement that I think. I can keep my file nicely ordered, and still have quick lookups.

Thanks,

Toolmaker



-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites