Jump to content
  • Advertisement
Sign in to follow this  
hades5k

Multimap tricky question

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I'm trying to code a simple inventory system. I wanna be able to add, delete item, retrieve info on a particular item, list all the items and list all the types with the quantity (ex: Armor: 4, Weapons : 2, etc.) Right now, I thought of using a multimap to store my item multimap<int, pair<ITEM_TYPE, CItem> > _mmInventory; the key is to retrieve a particular item, and the pair is composed of the type and the item. I dunno if there's a better way of using the multimap but my question is : how can I return an iterator to all the elements with a particular ITEM_TYPE? find() only query the key... Thanks!! Chris

Share this post


Link to post
Share on other sites
Advertisement
Maps are indexed by the key - you need a different data structure (such a boost::mulitindex)

Share this post


Link to post
Share on other sites
Ok, I'll have a look later!

But, let say I'm curious, is there a way to loop through the multimap looking only for the first part of my pair? (ITEM_TYPE)

Share this post


Link to post
Share on other sites
Quote:
Original post by hades5k
Ok, I'll have a look later!

But, let say I'm curious, is there a way to loop through the multimap looking only for the first part of my pair? (ITEM_TYPE)


Like this - but the performance isn't great (it's O(n) with respect to the size() of the map).


multimap<int, pair<ITEM_TYPE, CItem> > _mmInventory;

vector< multimap<int, pair<ITEM_TYPE, CItem> >::iterator > matchingIters;
multimap<int, pair<ITEM_TYPE, CItem> >::iterator it,endIt;
endIt = _mmInventory.end();
for(it=_mmInventory.begin();it!=endIt;++it)
{
if( (*it).second.first == REQUIRED_TYPE )
{
matchingIters.push_back(it);
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!