Skip to content

Instantly share code, notes, and snippets.

@socantre
Last active December 18, 2015 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save socantre/5851322 to your computer and use it in GitHub Desktop.
Save socantre/5851322 to your computer and use it in GitHub Desktop.
for_each_i: algorithm to iterator over a range, with an index.
template<class InputIterator, class Function>
Function for_each_i(InputIterator first, InputIterator last, Function f) {
using index_type = std::iterator_traits<InputIterator>::difference_type i;
for (index_type i {}; first!=last; ++first, ++i) {
f(*first, i);
}
return std::move(f);
}
/*
25.1
add:
template<class InputIterator, class Function>
Function for_each_i(InputIterator first, InputIterator last, Function f);
25.2.4
add:
template<class InputIterator, class Function>
Function for_each_i(InputIterator first, InputIterator last, Function f);
Effects: ... for_each_i passes, as a second argument to f, an object of type iterator_traits<InputIterator>::difference_type, which is initially value initialized and incremented after each application of f.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment