Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<class Iterator>
- class IteratorRange {
- public:
- IteratorRange(Iterator begin, Iterator end) : begin_(begin), end_(end) {}
- Iterator begin() const {
- return begin_;
- }
- Iterator end() const {
- return end_;
- }
- private:
- Iterator begin_;
- Iterator end_;
- };
- template<class T>
- class RangeIterator {
- public:
- RangeIterator(T current, T step) : current_(current), step_(step) {}
- RangeIterator& operator++() {
- current_ += step_;
- return *this;
- }
- bool operator!=(const RangeIterator &other) const {
- return current_ != other.current_;
- }
- auto operator*() const {
- return current_;
- }
- private:
- T current_;
- T step_;
- };
- template<class T>
- auto Range(T from, T to, T step) {
- return IteratorRange(RangeIterator(from, step), RangeIterator(to, step));
- }
- template<class Iterator>
- class GroupIterator {
- public:
- GroupIterator(Iterator current, Iterator end) : current_(current), end_(end) {}
- GroupIterator& operator++() {
- auto old_value = *current_;
- while (current_ < end_ && *current_ == old_value) {
- ++current_;
- }
- return *this;
- }
- bool operator!=(const GroupIterator& other) {
- return current_ != other.current_;
- }
- auto operator*() const {
- return *current_;
- }
- private:
- Iterator current_;
- Iterator end_;
- };
- template <class Iterator>
- auto Group(Iterator begin, Iterator end) {
- return IteratorRange(GroupIterator(begin, end), GroupIterator(end, end));
- }
- int main() {
- for (auto value: Range(1, 5, 1)) {
- std::cout << value << " ";
- }
- std::cout << "\n";
- std::vector<int> v{1, 1, 1, 2, 2, 3, 1, 1, 2, 2, 3};
- for (auto value : Group(v.begin(), v.end())) {
- std::cout << value << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement