Skip to content

Instantly share code, notes, and snippets.

@MikuroXina
Created December 12, 2018 04:26
Show Gist options
  • Save MikuroXina/83197702598b0abdd39c4505eab9ef2f to your computer and use it in GitHub Desktop.
Save MikuroXina/83197702598b0abdd39c4505eab9ef2f to your computer and use it in GitHub Desktop.
Indexed for each in C++
#ifndef FOR_INDEXED_LIB
#define FOR_INDEXED_LIB
// Usage: for_indexed(begin, end, [](auto &e, auto i) { ~: });
template<class ForwardIterator, class Func>
void for_indexed(ForwardIterator head, ForwardIterator tail, Func func) {
size_t i = 0;
for (auto it = head; it != tail; ++it, ++i) func(*it, i);
}
#endif // FOR_INDEXED_LIB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment