Jump to content
  • Advertisement


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

Pure Krome

STL #include <map> Questions

This topic is 5993 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 All. I have two quick questions. 1) How do i remove an item from a ''map''? eg. here is my INSERT / ADD function.
void CLists::AddPlayer (Player player)
  idnum_t iPlayerID = 0;

  iPlayerID = player.GetID ();
  playerMap.insert (PlayerMap::value_type (iPlayerID, player)); // Add the Player to the (player) Map

2) Is it possible to control the -size- of a ''map''. For example. i do not wish to always insert / add my CLASS to the ''map'' if my map already has .. hmm. lets say 1000 items (in this case, [class Player] players) in it. 3) If the answer to #2 is YES, then how do you know which item to remove from the map? i mean, can you add a unique value to the item that defines it''s IMPORTANCE? so the least important gets removed, and then the new item inserted / added? -PK-

Share this post

Link to post
Share on other sites
1. Use map::erase. There are 3 forms:

void erase( iterator pos );
void erase( iterator first, iterator last );
size_type erase( const key_type& x );

2. Yes it''s possible, but only by coding your own logic to do this. It would probably be best to encapsulate your map as a member of another class, and add in precondition checks and appropriate logic to control the size.

3. You''re really asking about a priority_queue here. However, you could key the map on something that conveys the required information (perhaps just an incremental integer), and use the range form of erase() to delete elements which fall into the required criteria.

Very simple ideas lie within the reach only of complex minds.

Share this post

Link to post
Share on other sites
priority_queue offers no iterators, so you cannot erase anything but the top element from it.

The problem you''re having has a solution, I''m sure, but you have to tell us what behavior you''re trying to get. I''m not sure map''s the right way to go at all, or it might be and require either a multimap, some custom predicates and a different key structure, or both. Can you tell us what you''re trying to accomplish without regards to your current implementation?

Share this post

Link to post
Share on other sites
Yeah, Stoffel summed up what I was thinking, especially after seeing the other thread too... it looks like you''re thinking too ''deeply'' about how to use a given algorithm, without being sure that the algorithm you''re contemplating does exactly what you want it to.

There are specific reasons for choosing a map, or a vector, or a list, or a set, and any other structure. When we know exactly what you are trying to achieve, we can point you to the container or algorithm you need, and explain how to use it to achieve your goal.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post

Link to post
Share on other sites
Yep. you''re both right.

The MAP thread explains exactly what i need to do and i''m using it wonderfully (in other areas on my game). This thread here has also answered one of my problems, so these two threads have been 150% helpful... really really really helpful


but yeah. my original question, i''m now wondering if i''ve been thinking of the wrong algorithm.

I''m going to stop answering this thread, because the ERASE answer was what i was after.

The priorty thing is another issue and i''ll be talking about that more on the other thread.

thank you all



Share this post

Link to post
Share on other sites

  • 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!