Custom Iterator in C++

前端 未结 6 1298
醉梦人生
醉梦人生 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:38

    An iterator is just a class that supports a certain interface. At minimum, you will want to be able to:

    • increment and/or decrement it
    • dereference it to get the object it "points" to
    • test it for equality and inequality
    • copy and assign it

    Once you have a class that can do that sensibly for your collection, you will need to modify the collection to have functions that return iterators. At minimum you will want

    • a begin() function that returns an instance of your new iterator type positioned at the first element
    • an end() function that returns an iterator which is (possibly notionally) positioned at one past the end of the items in your container

提交回复
热议问题