iterator design help

From: Rex_chaos (rex_chaos_at_21cn.com)
Date: 10/20/03


Date: 20 Oct 2003 08:31:05 -0700

Hi there,
  I am writing an iterator for a container. Just like a typical
iterator,

template <typename ADT>
class MyIter
{
  ...
  MyContainer<ADT>& refc;
  public:
    MyIter(MyContainer<ADT>& cont) :refc(a) {...}

    MyIter& begin() {...; return *this;}
    MyIter& end() {...; return *this;}
    MyIter& operator++() {...; return *this;}
    ...
};

While MyIter is applied to a template algorithm like std::transform,
the copy constructor will be actived because of the reference to cont.
It lowers effeciency a lot. Maybe I should use a pointer as instead
for speed purpose

...
  MyContainer<ADT>* refc;
  public:
    MyIter(MyContainer<ADT>& cont) :refc(a) {...}
...

However, it is another problem. Suppose the container passed to the
iterator has been released, refc will then become an invalid pointer
!!! I can't release the pointer (refc) in the destructor because I
don't know if any object own the container.



Relevant Pages

  • Re: Maintance of c++ code
    ... iterator constant in the standard, ... template ... template <class Iter_> ... container, some only work with particular containers. ...
    (comp.object)
  • traits help
    ... I am learning template programming and there is a problem about ... Now consider a container and an iterator. ... template <typename T> ...
    (comp.lang.cpp)
  • Re: iterator design help
    ... > I am writing an iterator for a container. ... > class MyIter ... refc will then become an invalid pointer ...
    (comp.lang.cpp)
  • Re: iterator design help
    ... >> class MyIter ... > the container, not of the iterator. ... Should I have "const" here like ...
    (comp.lang.cpp)
  • Re: Template parameter that has a template argument list
    ... > container that holds a specific type. ... Of course, I am speaking of (template template) parameters, ... > void DoSomething(Iterator Begin, ...
    (alt.comp.lang.learn.c-cpp)