Topic: Clearing the STL-containers


Author: Theodore Todorov <todorov@sbghp10.in2p3.fr>
Date: 1998/01/27
Raw View
Bradd W. Szonye wrote:
>
> Konstantin Baumann wrote in message <6aaa0g$h0d@netlab.cs.rpi.edu>...
> >Why doesn't the STL-containers have a "clear()"-method?
> >
> >This method could be implemented more efficiently in most cases (instead
> >of using "container.erase(container.begin(), container.end())").
> >
>
> It's also quite likely that the destructor for a container is no less
> complicated or more efficient than erase(begin(), end()). They both have
> to do the same work. If you mean "developer efficiency," that's really only
> an issue for the implementer.
>
> There's still the problem of the cumbersome calling syntax, which may be
> what you mean. However, if you read the CD2 carefully, you'll see that a
> clear() method is a container requirement for both sequences and
> associative
> containers. Perhaps you're reading from an older draft?
> ---

There is more to a true clear() method than just erase(begin(), end())
(the CD2 states that clear() is equivalent to erase(begin(), end()) ).
That is deallocation of any allocated storage: calling the standard
clear()
on a vector will not change it's capacity!

While I don't see how the efficiency of clear() can be improved
(since it must call the destructors of all container elements
no matter how it's implemented), I would like very much
to have a function that resets a container to the state where
it would be after the call to it's default constructor, freeing
all hidden (and unhidden) buffers.
To achieve this I am forced to use new to construct my
vector and to call delete and then new again to reset it!

I posted a question about a better way of achieving this
two days ago, but I never saw it in the news, so I'll
re-post it, sorry if it appears twice...

                  Teddy Todorov (todorov@sbghp10.in2p3.fr)

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: ark@research.att.com (Andrew Koenig)
Date: 1998/01/27
Raw View
In article <6aj9dl$pq0@netlab.cs.rpi.edu>,
Theodore Todorov  <todorov@sbghp10.in2p3.fr> wrote:

> While I don't see how the efficiency of clear() can be improved
> (since it must call the destructors of all container elements
> no matter how it's implemented), I would like very much
> to have a function that resets a container to the state where
> it would be after the call to it's default constructor, freeing
> all hidden (and unhidden) buffers.
> To achieve this I am forced to use new to construct my
> vector and to call delete and then new again to reset it!

I would expect that in many cases where you want to reset a
container that way, making it a local variable in a function
that returns and is called again would be just the thing.
--
    --Andrew Koenig
      ark@research.att.com
      http://www.research.att.com/info/ark


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Ryszard Kabatek <kabatek@chemie.uni-halle.de>
Date: 1998/01/29
Raw View
Theodore Todorov wrote:
>
> While I don't see how the efficiency of clear() can be improved
> (since it must call the destructors of all container elements
> no matter how it's implemented), I would like very much
> to have a function that resets a container to the state where
> it would be after the call to it's default constructor, freeing
> all hidden (and unhidden) buffers.
> To achieve this I am forced to use new to construct my
> vector and to call delete and then new again to reset it!
>

Hi Theodore,
I think a better idea is:

template <class Container> reset(Container& c)
{
  Container temp;
  c.swap(temp);
}

It's simple and fast :)

--
Ryszard Kabatek     ,,,
                   (o o)
---------------o00--(_)--00o---------------

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Konstantin Baumann" <kostab@uni-muenster.de>
Date: 1998/01/23
Raw View
Why doesn't the STL-containers have a "clear()"-method?

This method could be implemented more efficiently in most cases (instead
of using "container.erase(container.begin(), container.end())").

What about the following:

void any_container::clear() {
 any_container temp;
 swap(temp);
}

Yes I know I can turn this into a template-function:

template<class Container>
void clear(Container& c) {
 Container temp;
 c.swap(temp); // swap(c, temp) is also possible
               // but this would allow clear() to be applied
               // to non-container, too.
}

But I think this should be a member-function (like the "swap()"-method).

--
Konstantin Baumann                   Westfaelische Wilhelms-Universitaet
Institut fuer Informatik (Zi. 603),  Einsteinstr. 62,   D-48149 Muenster
mailto:kostab@math.uni-muenster.de                  Tel:+49-251-83-32701
http://wwwmath.uni-muenster.de/cs/u/kostab/         Fax:+49-251-83-33755


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]






Author: "Konstantin Baumann" <kostab@uni-muenster.de>
Date: 1998/01/24
Raw View
Why doesn't the STL-containers have a "clear()"-method?

This method could be implemented more efficiently in most cases (instead
of using "container.erase(container.begin(), container.end())").

What about the following:

void any_container::clear() {
 any_container temp;
 swap(temp);
}

Yes I know I can turn this into a template-function:

template<class Container>
void clear(Container& c) {
 Container temp;
 c.swap(temp); // swap(c, temp) is also possible
               // but this would allow clear() to be applied
               // to non-container, too.
}

But I think this should be a member-function (like the "swap()"-method).

--
Konstantin Baumann                   Westfaelische Wilhelms-Universitaet
Institut fuer Informatik (Zi. 603),  Einsteinstr. 62,   D-48149 Muenster
mailto:kostab@math.uni-muenster.de                  Tel:+49-251-83-32701
http://wwwmath.uni-muenster.de/cs/u/kostab/         Fax:+49-251-83-33755


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]





Author: mirven@uiuc.edu (Marcus B. Irven)
Date: 1998/01/25
Raw View
>Why doesn't the STL-containers have a "clear()"-method?

they do

>
>This method could be implemented more efficiently in most cases (instead
>of using "container.erase(container.begin(), container.end())").
>
>What about the following:
>
>void any_container::clear() {
> any_container temp;
> swap(temp);
>}
>

this wouldn't be any more efficient then:

void any_container::clear() { erase(begin(), end()); }

because temp would be destroyed before clear() returned.

Marcus Irven
mirven@uiuc.edu


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]





Author: "Bradd W. Szonye" <bradds@concentric.net>
Date: 1998/01/25
Raw View
Konstantin Baumann wrote in message <6aaa0g$h0d@netlab.cs.rpi.edu>...
>Why doesn't the STL-containers have a "clear()"-method?
>
>This method could be implemented more efficiently in most cases (instead
>of using "container.erase(container.begin(), container.end())").
>
>What about the following:
>
>void any_container::clear() {
> any_container temp;
> swap(temp);
>}

Of course, it's always possible that the library implements erase() as:

any_container::erase(iterator first, iterator last)
{
    if (first == begin() && last == end()) {
        any_container temp;
        swap(temp);
    }
    // ...
}

It's also quite likely that the destructor for a container is no less
complicated or more efficient than erase(begin(), end()). They both have
to
do the same work. If you mean "developer efficiency," that's really only
an
issue for the implementer.

There's still the problem of the cumbersome calling syntax, which may be
what you mean. However, if you read the CD2 carefully, you'll see that a
clear() method is a container requirement for both sequences and
associative
containers. Perhaps you're reading from an older draft?
---
Bradd W. Szonye
bradds@concentric.net
http://www.concentric.net/~Bradds



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]





Author: Matt Austern <austern@isolde.mti.sgi.com>
Date: 1998/01/25
Raw View
"Konstantin Baumann" <kostab@uni-muenster.de> writes:

> Why doesn't the STL-containers have a "clear()"-method?
>
> This method could be implemented more efficiently in most cases (instead
> of using "container.erase(container.begin(), container.end())").

They do.  All of the standard containers (vector, deque, list, set,
map, multiset, multimap) have a method called clear().

(Note that I've set followups to comp.std.c++ only.)




[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Pete Becker <petebecker@acm.org>
Date: 1998/01/25
Raw View
Konstantin Baumann wrote:
>
> Why doesn't the STL-containers have a "clear()"-method?
>

They do.


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]