Multimap tricky question

Started by
2 comments, last by Nitage 18 years, 7 months ago
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
Advertisement
Maps are indexed by the key - you need a different data structure (such a boost::mulitindex)
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)
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);    }}

This topic is closed to new replies.

Advertisement