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