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


Hi Julien,

julien2412 píše v Po 26. 08. 2013 v 21:41 -0700:

I'm taking a look to the use of erase on iterators.
I found this example:
    289 void OHierarchyElement_Impl::RemoveElement( const ::rtl::Reference<
OHierarchyElement_Impl >& aRef )
    290 {
    291     {
    292         ::osl::MutexGuard aGuard( m_aMutex );
    293         OHierarchyElementList_Impl::iterator aIter =
m_aChildren.begin();
    294         const OHierarchyElementList_Impl::const_iterator aEnd =
m_aChildren.end();
    295         while (aIter != aEnd)
    296         {
    297             if (aIter->second == aRef )
    298                 aIter = m_aChildren.erase(aIter);
    299             else
    300                 ++aIter;
    301         }
    302     }
See
http://opengrok.libreoffice.org/xref/core/package/source/xstor/ohierarchyholder.cxx#298

Is it ok to use "aEnd" or, since erase may  be called, we should change the
while into:
while (aIter != m_aChildren.end())
(and remove aEnd)

In this exact case (when the value may be present more times in the
vector), you might want to use the Erase-remove idiom [1]:

// remove all occurrences of aRef 
m_aChildren.erase(std::remove(m_aChildren.begin(), m_aChildren.end(), aRef), m_aChildren.end());

Even with the comment I suppose, so that people who haven't read tons of
C++ books can see what's going on ;-)

Another thing: couldn't we break the loop after erase or could aRef be
present several times?

Not sure - worth checking the history of that file I think.

[1] http://en.wikipedia.org/wiki/Erase-remove_idiom

Regards,
Kendy


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.