iterator help

R

Rex_chaos

Hi all,
I am writing my own container and need an iterator. I am hesitating
if I should my iterator should inherited from std::iterator or just
write my own one. Please give me an idea.

BTW, if I write my own one, should I let it defined inside my
container(inner class) or just be a complelely independent class.

Thanks.
 
D

David B. Held

Rex_chaos said:
I am writing my own container and need an iterator. I am
hesitating if I should my iterator should inherited from
std::iterator or just write my own one. Please give me an
idea.

Generally, you write an iterator that is most appropriate for
accessing the data in your container.
BTW, if I write my own one, should I let it defined inside my
container(inner class) or just be a complelely independent
class.

Most STL implementations define the iterator types as
nested classes. I prefer this technique myself, but I don't
believe there is any harm in defining it outside the container
class.

Dave
 
D

Dietmar Kuehl

I am writing my own container and need an iterator. I am hesitating
if I should my iterator should inherited from std::iterator or just
write my own one. Please give me an idea.

'std::iterator<>' just typedefs a bunch of types. That's it. If you
don't derive from 'std::iterator<>' you should be sure to [partially]
specialize 'std::iterator_traits<>' for your iterator type because
otherwise the algorithms won't work OK. Due to lack of partial
specialization support with some compilers, it is probably a good idea
BTW, if I write my own one, should I let it defined inside my
container(inner class) or just be a complelely independent class.

This one is a little bit tricky. First of all, it does not matter to
user. Since nested classes have no additional rights external class
have, nesting the iterator class only serves a documentation purpose.
With some specialization I found that it is sufficient to specialize
the container while leaving the iterator alone. Also, the semantics of
making an external class a friend are well defined in the standard
which does not apply to nested classes. Thus, I tend to make iterators
external classes. There is, however, no strong argument I'm aware of
which prefers one of the approaches.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top