Skip to content

Instantly share code, notes, and snippets.

@Ratstail91
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ratstail91/29550fecf5809ae057ee to your computer and use it in GitHub Desktop.
Save Ratstail91/29550fecf5809ae057ee to your computer and use it in GitHub Desktop.
A relatively simple function for selectively removing members of a std::map container.
#include <functional>
#include <map>
//this function takes "key, value" types of the map container as template parameters
//this function takes the map container and a lambda as arguments
//the lambda takes one argument, a std::pair structure which in turn takes "const key, const& value" as it's template parameters
template<typename key, typename value>
void map_remove_if(std::map<key, value>& container, std::function<bool(std::pair<key const, value const&>)> fn) {
std::map<key, value>::iterator it = container.begin();
while(it != container.end()) {
if (fn(*it)) {
it = container.erase(it);
}
else {
++it;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment