[ create a new paste ] login | about

Link: http://codepad.org/vzR5ubse    [ raw code | output | fork ]

C++, pasted on Jun 17:
#include <iostream>

template <typename T>
struct RIterator {
    RIterator(T *_data, size_t _size) 
        : data(_data), size(_size), 
          position(_data + _size - 1) {}
    
    /* Iterator starts at end of array */
    void operator++()
    {
        --position;
    }

    void operator--()
    {
        ++position;
    }

    bool at_end() { return (position == data) ? true : false; }
    T *get_data() { return position; }

private:
    T *data;
    size_t size;
    T *position;
};

int main()
{
    const char bob[] = "abcdefg";

    RIterator<const char> iterator(bob, sizeof(bob) / sizeof(bob[0]));

    for (; !iterator.at_end(); ++iterator) {
        std::cout << "Current Element: " << iterator.get_data()[0] << std::endl;
    }

    std::cout << "Current Element: " << iterator.get_data()[0] << std::endl;
}


Output:
1
2
3
4
5
6
7
8
Current Element: 
Current Element: g
Current Element: f
Current Element: e
Current Element: d
Current Element: c
Current Element: b
Current Element: a


Create a new paste based on this one


Comments: