Created
February 9, 2015 13:34
-
-
Save bkuhns/c82e5dda4e63cdcf08a7 to your computer and use it in GitHub Desktop.
The pair2params() function allows you to use a more readable predicate for iterating over std::map's without having to define a custom algorithm. The first argument will "bind" to pair.first and the second argument to pair.second.
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 <map> | |
#include <utility> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
namespace { | |
auto pair2params = [](auto&& f) | |
{ | |
return [f](auto&& p) { | |
f(p.first, p.second); | |
}; | |
}; | |
} | |
int main() | |
{ | |
auto values = map<int, string>{ | |
{0, "hello"}, | |
{1, "world!"} | |
}; | |
for_each(begin(values), end(values), pair2params([](int i, const string& s) { | |
cout << i << ": " << s << endl; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment