Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Iterating in order over an unordered map


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Servant of the Lord   Crossbones+   -  Reputation: 24453

Like
0Likes
Like

Posted 30 October 2013 - 08:00 PM

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, 30 October 2013 - 08:05 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5571

Like
1Likes
Like

Posted 30 October 2013 - 08:03 PM

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.


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3 Servant of the Lord   Crossbones+   -  Reputation: 24453

Like
0Likes
Like

Posted 30 October 2013 - 09:48 PM

Alright, thanks.

 

I'm now using a temporary map of references:

std::map<Key, std::reference_wrapper<Value>> sortedMap(std::begin(unsortedMap), std::end(unsortedMap));

for(const auto &pair : sortedMap)
{ ... }

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#4 Ryan_001   Prime Members   -  Reputation: 1745

Like
3Likes
Like

Posted 30 October 2013 - 11:03 PM

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.



#5 wack   Members   -  Reputation: 1358

Like
0Likes
Like

Posted 03 November 2013 - 04:54 PM

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...




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS