Re: std::vector::clear() semantics

From: André Pönitz (poenitz_at_gmx.net)
Date: 10/15/03


Date: Wed, 15 Oct 2003 13:57:39 +0000 (UTC)

tom_usenet <tom_usenet@hotmail.com> wrote:
>>Even if the standard is silent on the capacity change, table 67 seems to
>>require clear() to be identical to erase(begin, end).
>
> It is, but the "as-if" rule. Conforming code cannont detect whether it
> calls erase(begin, end) or not.

I think I can:

        #include <vector>
        #include <iostream>

        int main()
        {
                        std::vector<int> v(10);
                        v.erase(v.begin(), v.end());
                        size_t s1 = v.capacity();
                        v.clear();
                        size_t s2 = v.capacity();
                        if (s1 != s2)
                                std::cout << "non conforming" << std::endl;
                        else
                                std::cout << "can't say anything..." << std::endl;
        }

So clear() does not behave "as-if" erase(...).

> No, since there is nothing saying that erase(begin, end) must always
> perform the same with respect to capacity.

I read table 67 as 'clear()' behaves 'as-if' 'erase(begin, end)'.

It does not say anything about the capacity, but whatever happens to
the capacity in erase() should happen to it in clear().

> The output of the posted program would not have to be "10 10" or "0 0"
> even if both calls were to erase(begin, end).

Indeed. Anything of the form 'n n' might be ok (unless some other rule
forbids is)

> It could be "10 100", "10 0", "0 10", or pretty much anything else.
> For example, vector could legally use a random number to determine
> the capacity to allocate when it is emptied.

Interesting twist ;-)

What about the code above using just one vector?

Andre'



Relevant Pages

  • Re: Usage of KB and KiB
    ... until M$ came along, and before KiB became a standard, I was taught the convention as: ... Of course the best is unambiguous convention, like Ki,Mi,Gi etc. ... They even tried to use "unformatted capacity", but quickly gave up since, noone could use such capacity. ... 1024 in their arithmetic to calculate the number they report to you on memory or disk usage. ...
    (bit.listserv.ibm-main)
  • Re: An eastern excursion - a.k.a another long-winded trip report
    ... Well, I think this is a sensible way to increase capacity, unlike seat ... retain a buffet and increase standard capacity by converting TRFBs/ ... seats became 2 + 2 standard seats. ... henceforth into the adjacent first class saloon. ...
    (uk.railway)
  • Re: std::vector::clear() semantics
    ... I can find no such explicit requirement in the Standard. ... [The standard does not mention a function _decreasing_ capacity() so I ... I don't think this would work, as clearinvalidates your iterators. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: I wish DVD-ROMs would hurry up and kill off CD-ROMs.
    ... > Having 6X the capacity is not significant when the last technology ... > change resulted in over 450X the capacity. ... shows the medium needs to progress up to the higher standard." ... have been included as standard computer components for the last three years. ...
    (comp.sys.ibm.pc.games.action)
  • Re: Forth Frustrations
    ... that doing so would necessarily change the current radix. ... isn't read-only unless the standard says it is. ... say BASE has the capacity to change the number radix. ... and I'm wasting time bothering to reply to this foolishness. ...
    (comp.lang.forth)