Skip to content

Instantly share code, notes, and snippets.

@kvk1920
Last active May 20, 2018 22:19
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 kvk1920/4459158464bc3017e8bcca088379c38a to your computer and use it in GitHub Desktop.
Save kvk1920/4459158464bc3017e8bcca088379c38a to your computer and use it in GitHub Desktop.
template <typename Iterator, typename IteratorType>
struct __advance {
static Iterator f(Iterator it, int n) {
while (n > 0) { ++it; --n; }
while (n < 0) { --it; ++n; }
return it;
}
};
template <typename Iterator>
struct __advance<Iterator, std::random_access_iterator_tag> {
static Iterator f(Iterator it, int n) {
return it + n;
}
};
template <typename Iterator>
Iterator advance(Iterator it, int n) {
return
__advance
<
Iterator,
typename std::iterator_traits<Iterator>::iterator_category
>::f(it, n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment