Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #define in :
- class range
- {
- public:
- constexpr range(int end):
- _i(0),
- _end(end),
- _step(1),
- _sup(end >= 0)
- {}
- constexpr range(int begin, int end, unsigned int step=1):
- _i(begin),
- _end(end),
- _step(step),
- _sup(end >= begin)
- {}
- constexpr const range& begin()
- {
- return *this;
- }
- constexpr const range& end()
- {
- return *this;
- }
- constexpr bool operator!=(const range&)
- {
- return _sup ? (_i < _end) : (_i > _end);
- }
- void operator++()
- {
- if (_sup)
- {
- _i += _step;
- }
- else
- {
- _i -= _step;
- }
- }
- constexpr const int& operator*()
- {
- return _i;
- }
- private:
- int _i;
- const int _end;
- const unsigned int _step;
- const bool _sup;
- };
- int main()
- {
- for (const int& i in range(20, -20, 5))
- {
- std::cout << i << std::endl;
- if (i == 30) break;
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement