| 发表于:2007-01-16 22:28:4236楼 得分:1 |
"michael andersson " <a98mi...@ida.his.se> wrote in message news:3eca8b4a.4020204@ida.his.se... > does the stl specification say anything about whether or not > vector::clear will release allocated memory when called? or is this only > performed when reserve is called with a value less than capacity? none of these will. the common idiom to clear a vector and release the memory it allocated is: vector <theitemtype> ().swap( thevectortoclear ); this creates an empty temporary vector and swaps its contents with those of the provided variable. the destructor of the temporary will then typically release memory previously associated with the variable. it is not formally guaranteed to work -- but in practice it does. (and because it has become a common c++ idiom, i do not think that a library implementer would want to provide a different behavior). hth, -- ivan vecerina, dr. med. <> http://www.post1.com/~ivec soft dev manger, xitact <> http://www.xitact.com brainbench mvp for c++ <> http://www.brainbench.com | | |
|