Last active
August 29, 2015 14:13
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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