-
-
Save andrey-malets/fbca763a3d1824a6b66c4a986e2d1f12 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <iostream> | |
#include <iterator> | |
#include <list> | |
template<typename It> | |
size_t DistanceImpl(It begin, It end, std::forward_iterator_tag) { | |
size_t result = 0; | |
while (begin++ != end) | |
++result; | |
return result; | |
} | |
template<typename It> | |
size_t DistanceImpl(It begin, It end, std::random_access_iterator_tag) { | |
return end - begin; | |
} | |
template<typename It> | |
size_t Distance(It begin, It end) { | |
using Tag = typename std::iterator_traits<It>::iterator_category; | |
return DistanceImpl<It>(begin, end, Tag()); | |
} | |
int main(void) { | |
int x[] = {1, 2, 3}; | |
std::list<int> l(x, x+3); | |
return Distance(l.begin(), l.end()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment