Last active
April 12, 2018 05:45
-
-
Save kazeto/04e826ee9a2a24488bd03f39d64d5e89 to your computer and use it in GitHub Desktop.
Simple Python-like range function for the range-based for statement in C++11.
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
template <class T> class range | |
{ | |
public: | |
template <class T> class iterator | |
{ | |
public: | |
iterator(T c) : m_count(c) {} | |
T operator*() const { return m_count; } | |
void operator++() { ++m_count; } | |
bool operator!=(const iterator &x) { return (**this) != (*x); } | |
private: | |
T m_count; | |
}; | |
range(T end) : m_begin(0), m_end(end) {} | |
range(T begin, int end) : m_begin(begin), m_end(end) {} | |
iterator<T> begin() { return iterator<T>(m_begin); } | |
iterator<T> end() { return iterator<T>(m_end); } | |
private: | |
T m_begin; | |
T m_end; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment