Creating my own Iterators

前端 未结 6 1709
鱼传尺愫
鱼传尺愫 2020-11-29 15:58

I\'m trying to learn C++ so forgive me if this question demonstrates a lack of basic knowledge, you see, the fact is, I have a lack of basic knowledge.

I want some h

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 16:26

    /EDIT: I see, an own iterator is actually necessary here (I misread the question first). Still, I'm letting the code below stand because it can be useful in similar circumstances.


    Is an own iterator actually necessary here? Perhaps it's sufficient to forward all required definitions to the container holding the actual Points:

    // Your class `Piece`
    class Piece {
    private:
        Shape m_shape;
    
    public:
    
        typedef std::vector::iterator iterator;
        typedef std::vector::const_iterator const_iterator;
    
        iterator begin() { return m_shape.container.begin(); }
    
        const_iterator begin() const { return m_shape.container.begin(); }
    
        iterator end() { return m_shape.container.end(); }
    
        const_iterator end() const { return m_shape.const_container.end(); }
    }
    

    This is assuming you're using a vector internally but the type can easily be adapted.

提交回复
热议问题