Created
May 24, 2013 01:18
-
-
Save kmcallister/5640703 to your computer and use it in GitHub Desktop.
C++ range class
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
#include <iostream> | |
template <typename T> | |
class range { | |
private: | |
class iter { | |
private: | |
T m_x; | |
public: | |
iter(T x) : m_x(x) { } | |
void operator++() { m_x++; } | |
const T& operator*() const { return m_x; } | |
bool operator!=(const iter &other) const { | |
return m_x != other.m_x; | |
} | |
}; | |
T m_begin; | |
T m_end; | |
public: | |
iter begin() const { return iter(m_begin); } | |
iter end() const { return iter(m_end ); } | |
range(T range_begin, T range_end) | |
: m_begin(range_begin), m_end(range_end) | |
{ } | |
}; | |
int main() { | |
for (auto i : range<int>(1, 10)) { | |
std::cout << i << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment