Last active
May 20, 2018 22:18
-
-
Save kvk1920/4d1802c00068d4f0248d6b9de1a2d91f to your computer and use it in GitHub Desktop.
This file contains 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 Iterator, typename IteratorType> | |
struct __distance { | |
static typename std::iterator_traits<Iterator>::difference_type | |
f(Iterator l, Iterator r) { | |
typename std::iterator_traits<Iterator>::difference_type result(0); | |
while (l != r) { | |
++l; | |
++result; | |
} | |
return result; | |
} | |
}; | |
template <typename Iterator> | |
struct __distance<Iterator, std::random_access_iterator_tag> { | |
static typename std::iterator_traits<Iterator>::difference_type | |
f(Iterator l, Iterator r) { | |
return r - l; | |
} | |
}; | |
template <typename Iterator> | |
typename std::iterator_traits<Iterator>::difference_type distance(Iterator l, Iterator r) { | |
return __distance<Iterator, typename std::iterator_traits<Iterator>::iterator_category>::f(l, r); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment