Skip to content

Instantly share code, notes, and snippets.

@SHyx0rmZ
Created January 21, 2014 21:46
Show Gist options
  • Save SHyx0rmZ/8549058 to your computer and use it in GitHub Desktop.
Save SHyx0rmZ/8549058 to your computer and use it in GitHub Desktop.
For when you miss Ruby's map_with_index
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