Custom Iterator in C++

前端 未结 6 1302
醉梦人生
醉梦人生 2020-11-28 04:57

I have a class TContainer that is an aggregate of several stl collections pointers to TItems class.

I need to create an Iterator to traverse the elements in all the

6条回答
  •  时光说笑
    2020-11-28 05:28

    When I did my own iterator (a while ago now) I inherited from std::iterator and specified the type as the first template parameter. Hope that helps.

    For forward iterators user forward_iterator_tag rather than input_iterator_tag in the following code.

    This class was originally taken from istream_iterator class (and modified for my own use so it may not resemble the istram_iterator any more).

    template
    class _iterator
             :public std::iterator // Info about iterator
    {
        public:
            const T& operator*() const;
            const T* operator->() const;
            __iterator& operator++();
            __iterator operator++(int);
            bool equal(__iterator const& rhs) const;
    };
    
    template
    inline bool operator==(__iterator const& lhs,__iterator const& rhs)
    {
        return lhs.equal(rhs);
    }
    

    Check this documentation on iterator tags:
    http://www.sgi.com/tech/stl/iterator_tags.html

    Having just re-read the information on iterators:
    http://www.sgi.com/tech/stl/iterator_traits.html

    This is the old way of doing things (iterator_tags) the more modern approach is to set up iterator_traits<> for your iterator to make it fully compatible with the STL.

提交回复
热议问题