Connecting Tech Pros Worldwide Help | Site Map

std::vector resize(0) vs clear()

atreya
Guest
 
Posts: n/a
#1: Jul 18 '06
Hi,

I'm trying to figure out if there are any differences between the
implementation of resize(0) and clear() that a programmer using
std::vector should be aware of!

Could there be any advantage/disadvantage of using one over the other?

I checked the VS7.1 implementation, in which I couldn't find any
difference. Both of them are effectively calling erase(begin(), end()).

Thanks,
../Chaitanya Atreya

Howard Hinnant
Guest
 
Posts: n/a
#2: Jul 18 '06

re: std::vector resize(0) vs clear()


In article <1153259405.497479.200840@b28g2000cwb.googlegroups .com>,
"atreya" <atreya@gmail.comwrote:
Quote:
Hi,
>
I'm trying to figure out if there are any differences between the
implementation of resize(0) and clear() that a programmer using
std::vector should be aware of!
>
Could there be any advantage/disadvantage of using one over the other?
>
I checked the VS7.1 implementation, in which I couldn't find any
difference. Both of them are effectively calling erase(begin(), end()).
clear() is potentially more efficient than resize(0), both in terms of
speed and code size.

resize must check whether it is increasing in size or decreasing, and be
prepared to append to the end, or erase from the end (so all of that
code needs to be instantiated).

clear() knows right up front (at vector design time) that it has the
semantics of erase(begin(), end()). Actually calling erase(begin(),
end()) is suboptimal (imho). Indeed if you have a vector<Twhere T has
a trivial destructor (which can be determined at compile time for at
least a subset of types - scalars), then all vector should do is set
size() to 0 - job done. No if statement (which can empty a pipeline),
no looping, just a single assignment statement - maybe two inlined
assembly instructions. -- Implementations may differ.

-Howard
Jon Clements
Guest
 
Posts: n/a
#3: Jul 18 '06

re: std::vector resize(0) vs clear()



atreya wrote:
Quote:
Hi,
>
I'm trying to figure out if there are any differences between the
implementation of resize(0) and clear() that a programmer using
std::vector should be aware of!
>
Could there be any advantage/disadvantage of using one over the other?

Use clear() when you explicitly wish to make it clear you're erasing
all elements of the vector. Use resize() to make it clear you're
changing the size of the vector (either shorter or longer...).
Advantages are clarity, disadvantages are none... Even if the
implementation has a special rule case for an empty vector.

Jon

Jon Clements
Guest
 
Posts: n/a
#4: Jul 18 '06

re: std::vector resize(0) vs clear()



Jon Clements wrote:
Quote:
>.Even if the implementation has a special rule case for an empty vector.
I meant to say: if resize(0) checked to see if the resultant size of
the vector should be empty (so it might choose to call clear()). Not if
the vector was already empty.

As Howard has already mentioned, resize needs to be smarter, so if you
want to empty the vector use the method designed for it! Don't worry
about the implementation!

Jon.

atreya
Guest
 
Posts: n/a
#5: Jul 18 '06

re: std::vector resize(0) vs clear()


Howard Hinnant wrote:
Quote:
In article <1153259405.497479.200840@b28g2000cwb.googlegroups .com>,
"atreya" <atreya@gmail.comwrote:
>
Quote:
Hi,

I'm trying to figure out if there are any differences between the
implementation of resize(0) and clear() that a programmer using
std::vector should be aware of!

Could there be any advantage/disadvantage of using one over the other?

I checked the VS7.1 implementation, in which I couldn't find any
difference. Both of them are effectively calling erase(begin(), end()).
>
clear() is potentially more efficient than resize(0), both in terms of
speed and code size.
>
resize must check whether it is increasing in size or decreasing, and be
prepared to append to the end, or erase from the end (so all of that
code needs to be instantiated).
>
clear() knows right up front (at vector design time) that it has the
semantics of erase(begin(), end()). Actually calling erase(begin(),
end()) is suboptimal (imho). Indeed if you have a vector<Twhere T has
a trivial destructor (which can be determined at compile time for at
least a subset of types - scalars), then all vector should do is set
size() to 0 - job done. No if statement (which can empty a pipeline),
no looping, just a single assignment statement - maybe two inlined
assembly instructions. -- Implementations may differ.
>
-Howard
About resize checking whether it is increasing/decreasing etcetera, and
instantiating code, there is no overhead at all except for a single if
statement as follows:
if (_Newsize < size())
where size() can be inlined (single line).

../Chaitanya Atreya

James Bannon
Guest
 
Posts: n/a
#6: Jul 19 '06

re: std::vector resize(0) vs clear()


atreya wrote:
Quote:
Hi,
>
I'm trying to figure out if there are any differences between the
implementation of resize(0) and clear() that a programmer using
std::vector should be aware of!
>
Could there be any advantage/disadvantage of using one over the other?
>
I checked the VS7.1 implementation, in which I couldn't find any
difference. Both of them are effectively calling erase(begin(), end()).
>
Thanks,
./Chaitanya Atreya
>
I'd go with Jon here: one function, one responsibility. Doing otherwise
is confusing and could lead to surprises. This would be like relying on
the interface to realloc () to allocate, delete and resize memory all in
one function.

Cheers
Jim.
Closed Thread