Boost logo

Boost :

From: Hayati Ayguen (Ayguen_at_[hidden])
Date: 2001-07-17 02:17:33


Hello,

i don't think your approch is a better one. you're replacing one problem
with another one:

1-
  c.erase(it++)

has just the problem of foreholding the old value. this needs space and
a kind of copy operator has to be called internally.

2-
calling erase and increment seperately would solve the problem:

  c.erase(it);
  ++it;

this doesn't look fine, but it works: neither space nor time is
required.

3-

  it = c.erase(it);

solves the space problem, but not the copy/define operation! for complex
iterators where the iterator class has many member variables, one should
avoid such a design, where an implicit or explicit copy has to be
called.

4-
if you find (2) a problem, you may define erase() in the iterator class
internally calling the princrement operator but returning "void":

  void it.erase();

  void Iterator::erase()
  {
    Container.erase(*this);
    ++(*this);
  }

but this would break the semantics of erase() from namespace std.

Hayati Ayguen

>
> Hi All,
> IMO, the current state of affairs for erasing elements from a
> container is somewhat unsatisfactory for average programmers.
>
> For some of the containers (std::set, std::map), erase is a "void"
> method - in which case the correct action is to keep a copy of the
> increment iterator beforehand, like this:
>
> c.erase(it++)
>
> But for other containers (e.g. std::vector), the correct action is to
> take the return value of erase:
>
> it = c.erase(it);
>
>
> ....
>
> Paul Hollingsworth
> http://PaulHollingsworth.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk