Skip to content

Instantly share code, notes, and snippets.

@kazeto
Last active April 12, 2018 05:45
Show Gist options
  • Save kazeto/04e826ee9a2a24488bd03f39d64d5e89 to your computer and use it in GitHub Desktop.
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.
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