Date: prev next · Thread: first prev next last
2012 Archives by date, by thread · List index


On 03/02/12 14:01, Stephan Bergmann wrote:
On 02/02/2012 09:08 PM, julien2412 wrote:
Would this patch better ? (I kept the for loop)

Unfortunately that still has a problem.  After "rBoxes.erase(toErase)", 
"it" (which is the same as "toErase") is invalidated, so incrementing it 
(up in the for(...;...;...) part) has undefined behavior.

The "standard idiom" is

   for (iterator i = m.begin(); i != m.end();) {
     if (doErase) {
       m.erase(i++);
     } else {
       ++i;
     }
   }

but doesn't that have the same problem? "i" is incremented only after
the erase is complete, when "i" is already invalid.

shouldn't this be something like:

    for (iterator i = m.begin(); i != m.end(); ) {
        if (doErase) {
           iterator const j = i++;
           m.erase(j);
        } else {
           ++i;
        }
    }

I read that to erase a position on a iterator invalidate this iterator from
position to the end
(http://www.cplusplus.com/reference/stl/vector/erase/)
I don't know if to create this temporary variable "toErase" change something
or it's all the same.

The text you cite is about vector, for which *all* iterators into a 
vector are invalidated upon an erase.

i think i read this "invalidate from position to end" in the SGI
documentation as well; does the actual standard give implementations
more freedom here?



Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.