Thanks Richard and zap. After posting, I did notice the return value from erase, and decided to use a while loop. However, I still wonder about the details of the standard and what is correct. 1) is end unchanging, or must you call end() every time rather than remembering it? 2) does resuming iteration from the "successor" traverse the remaining elements? The wording is copied from the plain map class, which is ordered and the ordering definition of the iterators makes #2 well defined. That is not the case for the unordered_map. I might argue that even "successor" is not defined on this type. I understand that the intent is to work like map, but have the ordering be opaque to the user. But, as documented, and now in the standard that I found online, I'm afraid that a conforming implementation could be different. E.g. removing an element could totally re-order the apparent traversal order, and even though the iterators are still valid, will not pick up exactly those elements that were not already seen. --John
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Richard Hadsell Sent: Monday, August 31, 2009 6:12 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Correct use of unordered_map
John Dlugosz wrote:
Consider this loop:
const my_map_t::iterator end= my_map.end(); for (my_map_t::iterator it= my_map.begin(); it != end; ++it) { if (is_to_be_removed(it->first)) global_map.erase (it); }
The documentation I've seen for unordered_map says that removing doesn't invalidate iterators except those referring to the element being deleted. But that's the case for it itself! Also, what does removing one element do to the ordering of the elements -- is there any guarantee that the iterator will visit exactly the ones that were not visited before?
What is the correct, standards-conforming, way to perform this?
I use one of these 2 ways:
for (my_map_t::iterator it= my_map.begin(); it != end; ) { if (is_to_be_removed(it->first)) it = global_map.erase (it); else ++it; }
for (my_map_t::iterator it= my_map.begin(); it != end; ) { my_map_t::iterator itx = it++; if (is_to_be_removed(itx->first)) global_map.erase (itx); }
-- Dick Hadsell 203-992-6320 Fax: 203-992-6001 Reply-to: hadsell@blueskystudios.com Blue Sky Studios http://www.blueskystudios.com 1 American Lane, Greenwich, CT 06831-2560
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
TradeStation Group, Inc. is a publicly-traded holding company (NASDAQ GS: TRAD) of three operating subsidiaries, TradeStation Securities, Inc. (Member NYSE, FINRA, SIPC and NFA), TradeStation Technologies, Inc., a trading software and subscription company, and TradeStation Europe Limited, a United Kingdom, FSA-authorized introducing brokerage firm. None of these companies provides trading or investment advice, recommendations or endorsements of any kind. The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.