Index: include/llvm/ADT/STLExtras.h =================================================================== --- include/llvm/ADT/STLExtras.h +++ include/llvm/ADT/STLExtras.h @@ -852,6 +852,13 @@ return std::find_if_not(std::begin(Range), std::end(Range), P); } +/// Provide wrapper to std::for_each which take ranges instead of having to +/// pass begin/end explicitly. +template +UnaryFunction for_each(R &&Range, UnaryFunction F) { + return std::for_each(std::begin(Range), std::end(Range), F); +} + /// Provide wrappers to std::remove_if which take ranges instead of having to /// pass begin/end explicitly. template Index: unittests/ADT/STLExtrasTest.cpp =================================================================== --- unittests/ADT/STLExtrasTest.cpp +++ unittests/ADT/STLExtrasTest.cpp @@ -318,4 +318,14 @@ EXPECT_EQ(7, V[3]); } +TEST(STLExtrasTest, ForEach) { + std::vector> V = {{1, 2}, {3, 4}, {5, 6}}; + std::vector Evens{}; + auto BackInserter = std::back_inserter(Evens); + for_each(V, + [&](std::pair Element) { BackInserter = Element.second; }); + std::vector ExpectedEvens = {2, 4, 6}; + EXPECT_EQ(Evens, ExpectedEvens); +} + }