Re: std::vector::clear() semantics
From: André Pönitz (poenitz_at_gmx.net)
Date: 10/15/03
- Next message: Huub: "Re: reading time with time.h?"
- Previous message: WW: "Re: Where to Start?"
- In reply to: tom_usenet: "Re: std::vector::clear() semantics"
- Next in thread: tom_usenet: "Re: std::vector::clear() semantics"
- Reply: tom_usenet: "Re: std::vector::clear() semantics"
- Reply: Jerry Coffin: "Re: std::vector::clear() semantics"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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'
- Next message: Huub: "Re: reading time with time.h?"
- Previous message: WW: "Re: Where to Start?"
- In reply to: tom_usenet: "Re: std::vector::clear() semantics"
- Next in thread: tom_usenet: "Re: std::vector::clear() semantics"
- Reply: tom_usenet: "Re: std::vector::clear() semantics"
- Reply: Jerry Coffin: "Re: std::vector::clear() semantics"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|