Skip to content

Instantly share code, notes, and snippets.

@kmcallister
Created May 24, 2013 01:18
Show Gist options
  • Save kmcallister/5640703 to your computer and use it in GitHub Desktop.
Save kmcallister/5640703 to your computer and use it in GitHub Desktop.
C++ range class
#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