Creating my own Iterators

前端 未结 6 1703
鱼传尺愫
鱼传尺愫 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:20

    The solution to your problem is not the creation of your own iterators, but the use of existing STL containers and iterators. Store the points in each shape in a container like vector.

    class Shape {
        private:
        vector  points;
    

    What you do from then on depends on your design. The best approach is to iterate through points in methods inside Shape.

    for (vector ::iterator i = points.begin(); i != points.end(); ++i)
        /* ... */
    

    If you need to access points outside Shape (this could be a mark of a deficient design) you can create in Shape methods that will return the iterator access functions for points (in that case also create a public typedef for the points container). Look at the answer by Konrad Rudolph for details of this approach.

提交回复
热议问题