Skip to content

Instantly share code, notes, and snippets.

@bkuhns
Created February 9, 2015 13:34
Show Gist options
  • Save bkuhns/c82e5dda4e63cdcf08a7 to your computer and use it in GitHub Desktop.
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.
#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