iterator help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rex_chaos

    iterator help

    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.
  • David B. Held

    #2
    Re: iterator help

    "Rex_chaos" <rex_chaos@21cn .com> wrote in message
    news:f7a7417.03 10082136.64690c 9@posting.googl e.com...[color=blue]
    >
    > 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.[/color]

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

    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



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003


    Comment

    • Dietmar Kuehl

      #3
      Re: iterator help

      rex_chaos@21cn. com (Rex_chaos) wrote:[color=blue]
      > 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.[/color]

      '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
      to derive from 'std::iterator< >' anyway: originally, this was mostly a
      work around.
      [color=blue]
      > BTW, if I write my own one, should I let it defined inside my
      > container(inner class) or just be a complelely independent class.[/color]

      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.
      --
      <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
      Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.co m/>

      Comment

      Working...