Created
August 7, 2015 20:37
-
-
Save Naios/e6f6e0a2951a5f630c23 to your computer and use it in GitHub Desktop.
Using C++11 auto for syntax with ranges
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
#ifndef range_h__ | |
#define range_h__ | |
#include <iterator> | |
template<typename T> | |
class range_iterator | |
: public std::iterator<std::forward_iterator_tag, T> | |
{ | |
T _current; | |
T _step; | |
public: | |
range_iterator(T current, T step) | |
: _current(current), _step(step) { } | |
range_iterator& operator++() { _current += _step; return *this; } | |
bool operator==(range_iterator const& other) const { return _current == other._current; } | |
bool operator!=(range_iterator const& other) const { return !(*this == other); } | |
value_type operator*() const { return _current; } | |
value_type* operator->() const { return &_current; } | |
}; | |
template<typename T> | |
class range | |
{ | |
T _begin; | |
T _end; | |
T _step; | |
public: | |
range(T begin, T end, T step = 1) | |
: _begin(begin), _end(end), _step(step) { } | |
range_iterator<T> begin() | |
{ | |
return range_iterator<T>(_begin, _step); | |
} | |
range_iterator<T> end() | |
{ | |
return range_iterator<T>(_end, _step); | |
} | |
}; | |
template<typename T> | |
range<T> make_range(T begin, T end, T step = 1) | |
{ | |
return range<T>(begin, end, step); | |
} | |
#endif // range_h__ |
Author
Naios
commented
Aug 7, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment