From: Joe.Greer@Visionsolutions.com   
      
   "Peter Liedermann" wrote in   
   news:op.wdmhdkm0vc68pg@hp2011.workgroup:   
      
   > On Tue, 01 May 2012 00:41:37 +0200, Francis Glassborow   
   > wrote:   
   >   
   >> On 30/04/2012 21:33, Chris Vine wrote:   
   >> (...) In general the Standard does not specify how things   
   >> should be done only what the resulting behaviour should be.   
   >>   
   >   
   > This takes me back to my original question. I have no access to the   
   > standard, but as far as I know, it does specify the complexity of   
   > operations, and for anyVector.clear() it says O(1) plus whatever   
   > destruction is needed. In the case of vector anyVector   
   > destruction is a no-op (this does not seem to be disputed here). This   
   > would then preclude, in my opinion, iterating over the vector for the   
   > purpose of clear()-ing it, and that would not just be an efficiency   
   > issue of clever implementation.   
   >   
   > Regards   
   > Peter   
   >   
   >   
      
   The idiom I always used when I really want to clear a vector is (If I   
   can get this right off the top of my head):   
      
   template clear(std::vector & vec)   
   {   
    std::vector tmp;   
    vec.swap(tmp);   
   }   
      
   This has the advantage of freeing any allocated memory from the original   
   as well as emptying it. Of course, that is not always what you want, so   
   your mileage may vary. I haven't seen anyone post this yet, so I thought   
   I would.   
      
   joe   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|