实现一个跨步迭代器

#include 
#include 
#include 
#include 

using namespace std;

template
class kstride_iter
{
public:
    typedef typename std::iterator_traits::value_type value_type;
    typedef typename std::iterator_traits::reference reference;
    typedef typename std::iterator_traits::difference_type difference_type;
    typedef typename std::iterator_traits::pointer pointer;
    typedef std::random_access_iterator_tag iterator_category;
    typedef kstride_iter self;

    kstride_iter() : m(NULL) {}
    kstride_iter(const self& x) : m(x.m) {}
    explicit kstride_iter(Iter_T x) : m(x) {}

    self& operator++() { m += Step_N; return *this; }
    self operator++(int) { self tmp = *this; m += Step_N; return tmp; }
    self& operator+=(difference_type x) { m += x * Step_N; return *this; }
    self& operator--() { m -= Step_N; return *this; }
    self operator--(int) { self tmp = *this; m -= Step_N; return tmp; }
    self& operator-=(difference_type x) { m -= x * Step_N; return *this; }
    reference operator[](difference_type n) { return m[n*Step_N]; }
    reference operator*() { return *m; }

    friend bool operator==(self x, self y) { return x.m == y.m; }
    friend bool operator!=(self x, self y) { return x.m != y.m; }
    friend bool operator<(self x, self y) { return x.m < y.m; }
    friend difference_type operator-(self x, self y) { return (x.m - y.m) / Step_N; }
    friend self operator+(self x, difference_type y) { return x += y * Step_N; }
    friend self operator+(difference_type x, self y) { return y += x * Step_N; }
private:
    Iter_T m;
};

int main(int argc, char** argv)
{
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
    kstride_iter first(a);
    kstride_iter last(a+8);
    copy(first, last, ostream_iterator(cout, "\n"));
    system("pause");
    return 0;
}

你可能感兴趣的:(CV)