Skip to main content
GameDev.net gamedev.net
๐Ÿ”’ Locked

[C++] multimap remove_if

Started by ToohrVyk Aug 27, 2006 at 12:03 PM 1 replies 2.7k views
Original Post
ToohrVyk
ToohrVyk
I want to remove associations from a multimap under a given condition. What I would like my code to look like is as follows:

typedef std::multimap<std::pair<int,int>,BaseResolver*> resolvers_t;
resolvers_t resolvers;

void RemoveResolver(BaseResolver* theResolver) {
  std::remove_if(
    resolvers.begin(),
    resolvers.end(),
    std::compose1(
      std::bind_2nd(std::equal_to<BaseResolver*>(), theResolver),
      std::select2nd<resolvers_t::value_type>()));
}
Of course, the above does not work (simply because that's not what remove_if really does). Does the SC++L provide me with something like this, or do I have to manually traverse the multi_map and erase the individual iterators?
Blog โ€” Facebook
Conner McCloud
Conner McCloud
remove_copy_if would probably give the behavior you want. Something along these lines [untested]:

typedef std::multimap<std::pair<int,int>,BaseResolver*> resolvers_t;resolvers_t resolvers;void RemoveResolver(BaseResolver* theResolver) {  resolvers_t newMap;    std::remove_copy_if(    resolvers.begin(),    resolvers.end(),    std::insert_iterator<resolvers_t>(newMap, newMap.end()),    std::compose1(      std::bind_2nd(std::equal_to<BaseResolver*>(), theResolver),      std::select2nd<resolvers_t::value_type>()));  resolvers = newMap;}


CM
ToohrVyk
ToohrVyk
Thanks for your suggestion. Well then, I guess working with iterators will be both shorter and faster in this particular case.
Blog โ€” Facebook

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.