c++ - Vector clear vs. resize -
i read on internet if clearing std::vector
repetitively (in tight loop), might better use resize(0)
instead of clear()
, may faster. not sure this. have definitive answer this?
i assume mean resize(0)
instead of setsize
, , calling instead of clear()
, , you're talking std::vector
. iirc recent answer discussed (can't find link), , on modern stl implementations, clear()
identical resize(0)
.
previously clearing vector might have freed memory (ie. capacity falls zero), causing reallocations when start adding elements again, in contrast resize(0)
keeping capacity there fewer reallocations. however, think in modern stl libraries there no difference. if you're using old stl implementation, or you're paranoid, resize(0)
might faster.
Comments
Post a Comment