Created
January 21, 2014 21:46
-
-
Save SHyx0rmZ/8549058 to your computer and use it in GitHub Desktop.
For when you miss Ruby's map_with_index
This file contains hidden or 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
template <typename InputIt1, typename InputIt2, typename BinaryFunction> | |
auto map_with_index(InputIt1 first, InputIt2 last, BinaryFunction f) -> vector<typename iterator_traits<InputIt1>::value_type> | |
{ | |
vector<typename iterator_traits<InputIt1>::value_type> result(distance(first, last)); | |
size_t i = 0; | |
generate(begin(result), end(result), [&](){ | |
return f(*first++, i++); | |
}); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment