Implementing own iterator on vector

Hello guys!

I have a problem, or maybe some misunderstanding. I'm writing my semestral project to school. In short, we have to create model of "Santa sleight". It contains moose, reindeer .. SANTA .. technical vehicle and also vehicle with presents. This is no problem, I already have lot of work done.

But all of these objects (or better poiters) must be stored in some kind of template container, with given interface.
This is the interface: http://pastebin.com/mHXDHK1S

Well, for storing, we can use everything what we want (STL containers, array, linear list..). So as you can see, I'm using STL vector.

The problem is, that I don't know, what should I write into class iterator/reverse iterator also into that four methods. I know, that vector already have methods for iteration (.begin(), .end(), rbegin(), .rend()). Also if I want to remove some element from container, I first must set iterator on proper position and then send it through method parameter and and...

Well, i really don't know, how to continue. Could you guys help me with some hint?

Some of my friends do this by that linear list and already have their own iterator implemented. But why to create linear list and all the stuff, when you have STL containers. :-)

Thank you!
The simplest way is to simply make the iterator types typedefs of the vector's iterator type.

1
2
typedef std::vector<T*>::iterator iterator;
typedef std::vector<T*>::reverse_iterator reverse_iterator;
Peter87, thanks for the fast reply!

Ok, that looks ok. But if i paste it into my code and then try to build it, it comes up with lot of errors. It is not easy to implement for me, since i never used iterators before in c++ . :(

This is my code right now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<typename T, typename Comparer>
class StructuredContainer<T*, Comparer> {
private:
        std::vector<T*> _containerOfElements;
public:
        typedef std::vector<T*>::iterator iterator;
        typedef std::vector<T*>::reverse_iterator reverse_iterator;
        //class iterator;
        //class reverse_iterator;
 
        StructuredContainer();
 
        void Insert(T* element);
        void Remove(iterator it);
...
...


but there are lot's of error like this:
1
2
3
4
5
6
Error	C2238	unexpected token(s) preceding ';'
Error	C2061	syntax error: identifier 'iterator'	
Warning	C4346	'iterator': dependent name is not a type
Warning	C4346	'iterator': dependent name is not a type
Error	C2061	syntax error: identifier 'iterator'
Error	C2238	unexpected token(s) preceding ';'


Again, thank you for your patience with me. :-)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>

template< typename T, typename Comparer >
class StructuredContainer /* <T*, Comparer> */ {

    private:
            std::vector<T*> _containerOfElements;
    public:
            typedef typename std::vector<T*>::iterator iterator;
            typedef typename std::vector<T*>::reverse_iterator reverse_iterator;
            typedef typename std::vector<T*>::const_iterator const_iterator;
            typedef typename std::vector<T*>::const_reverse_iterator const_reverse_iterator;

            iterator begin() { return _containerOfElements.begin() ; }
            const_iterator cbegin() const { return _containerOfElements.begin() ; }
            const_iterator begin() const { return _containerOfElements.begin() ; }
            // etc.

            // ...

};


Dependant names: http://www.cplusplus.com/forum/general/169739/#msg848354
Thank you very much @JLBorges! It works. :-)

If I may ask, why did you corss out <T*, Comparer>?

I have it there, because this template is partial specialization for T*. :-)
> If I may ask, why did you corss out <T*, Comparer>?
> I have it there, because this template is partial specialization for T*. :-)

Well, if it is a partial specialisation, it should be there.
(I assumed that it wasn't, because the pastebin code did not have a declaration of the primary template.)
Topic archived. No new replies allowed.