Sign in to follow this  
Servant of the Lord

Iterating in order over an unordered map

Recommended Posts

I have an std::unordered_map<MyStruct>. It's unordered for extra-fast lookups which is what will be needed most of the time. However, once, at startup, I'd like to iterate over it in order of MyStruct::myInt.

 

The container currently contains upwards of 2000 elements, and might reach as high as 5-10 thousand.

 

Is there some way I can iterate over it in an order other than its internal hashing order?

I'm not worried about speed.

Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

No.

 

If you know the keys used you can sort them and then look up the items in order.

 

You could put them in a map and iterate over them (and use the map to fill the unordered_map) then throw the map away.

Share this post


Link to post
Share on other sites
wack    1358

I'm pretty sure boost::multi_index (http://www.boost.org/doc/libs/1_54_0/libs/multi_index/doc/index.html) can do what you're asking for.  You can set up a sequential index and an unordered index.


Thanks for the tip about multi_index. Just used it to clean up a monstrosity with multiple containers that I created a while back.

Boost contains so much, it's tricky to really get an overview of the stuff you acually need sometimes...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this