Last Updated: February 25, 2016
·
616
· msoucy

C++11 Wrappers

Make dummy classes with begin and end functions, and you can use them with C++11's range-based for loop to do a lot of the things available in Python's itertools module

class Count {
    int val, step;
public:
    class iterator {
        int val, step;
    public:
        iterator(int val, int step)
            : val(val), step(step)
        {
        }
        iterator operator++() {
            val+=step;
            return *this;
        }
        bool operator!=(const iterator& other) const {
            return (val+step > other.val);
        }
        int operator*() const { return val; }
    };
    Count(int v, int step=1) : val(v), step(step) {}
    iterator begin() { return iterator(val, step); }
    iterator end() { return iterator(val-1, step); }
};

for(int i : Count(5,3)) {
    cout << i << endl;
}