Re: clear() on vectors

From: joel (ploa_at_carrtol.com)
Date: 12/16/04


Date: Thu, 16 Dec 2004 20:51:59 +0000


>When you call it, it does what it's supposed to do (assuming
>the compiler works correctly, and the program hasn't already
>entered 'UB-land'). After it's called, it doesn't 'do' anything
>at all. How could it? It has already completed executing,
>and control has returned to its caller.
>
>>
>> i am trying to write a vector class that includes a erase and clear
>> function,
>
>
>Why not just use 'std::vector'? (or is this e.g. a learning
>exercise?)
>
>>without copying and ammending the header file.
>
>What header file? If you mean your own (which presumably
>contains the definition of your vector class), then if you
>want add, remove, or change functionality, then yes,
>of course you must make changes to the header (and the
>corresponding implementation file, if not everything is
>written inline in the class definition).
>
>>
>> as there a two overloaded functions for erase, i didnt know whether
>> clear() called the 2 iterator erase function,
>
>It's required to perform the equivalent of (or exactly):
>
> erase(begin(), end())
>
>This invocation might or might not actually be what it does
>'under the covers', it's merely required to behave 'as if'
>it does.
>
>
>> which as part of its
>> algorithm, was to erase each iterator
>
>No iterators are erased. The vector elements are erased.
>
>>by passing it to the other erase
>> function
>
>It would be valid for it to do that, but not required.
>
>>
>> in the vector include file for msvc++ 2003 it says
>>
>> void clear()
>> { // erase all elements
>> erase(begin(), end());
>> }
>
>That's very typical, and a valid way to do it.
>
>>
>>
>> in this erase function that takes two iterators does it clear the
>> values like
>>
>> while(i != j)
>> { *i = 0;
>> ++i;
>> }
>> return i;
>
>No. (Well, I supposed it could, in addition to the required
>behavior, but that would simply be a waste of time).
>
>Your above example *changes* the values, but 'clear()' *removes*
>(i.e. 'erases') them. The required post-condition of
>'clear()' (for any sequence container) is size() == 0.
>
>>
>> then call a memory management function to destroy and deallocate the
>> vector?
>
>As I've said before, the vector does handle its own
>memory managment. The actual details and mechanics
>of this are implementation dependent. (but you can
>provide your own allocator when you create the vector
>if you like -- I've never done this, so I'm not qualified
>to teach you the correct way to do it).
>
>I think you're getting too concerned about 'internal details'
>Most of the C++ language specifications deal with required
>*behavior*, not with *how* that behavior is achieved.
>
>-Mike
>
yes it is a learning exercise.

as clear() in effect calls erase(iterator, iterator) what i am looking
for i suppose is how does the erase(iterator, iterator) work.

i have already done the single version erase(iterator) function

erase(iterator i)
{
iterator a = i;
std::copy(++i, b, a);// b is a private member iterator of my vector
                                     class
 return a;
}

the problem is i dont know how to truncate the remaining element to
make the vector 1 element smaller afterwards.

therefore i cannot implement the other erase function without knowing
how to delete/destroy the elements of a vector to make it's size == 0;
(which i presume is what clear does)



Relevant Pages

  • Re: clear() on vectors
    ... >>sent to erase (taking one iterator) where it is destroyed? ... i am trying to write a vector class that includes a erase and clear ... then call a memory management function to destroy and deallocate the ...
    (alt.comp.lang.learn.c-cpp)
  • Re: clear() on vectors
    ... > i am trying to write a vector class that includes a erase and clear ... > clearcalled the 2 iterator erase function, ... > then call a memory management function to destroy and deallocate the ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Help with some logic, best way to update a container.
    ... you could use make_pair to avoid having to specify the template ... Now that I look at my code, I see that indeed I use the <iterator, bool> return from ... you might implement in a map, as 65=1 (treating the letters as integers because you gave ... the documentation is sloppy; the erase method does not state this behavior. ...
    (microsoft.public.vc.mfc)
  • Re: Help with some logic, best way to update a container.
    ... you could use make_pair to avoid having to specify the template ... you might implement in a map, as 65=1 (treating the letters as integers because you gave ... Incrementing iter is undefined after you erase it. ... For containers whose erase returns an iterator, ...
    (microsoft.public.vc.mfc)