Re: Erasing objects satisfying a criterion from std::map
From: Thomas Matthews (Thomas_MatthewsSpamBotsSuck_at_sbcglobal.net)
Date: 01/10/05
- Next message: Thomas Matthews: "Re: including a large constants file"
- Previous message: john.burton.email_at_gmail.com: "Re: Correct usage of "using" with standard templates"
- In reply to: KaizerSose_at_gmail.com: "Re: Erasing objects satisfying a criterion from std::map"
- Next in thread: Victor Bazarov: "Re: Erasing objects satisfying a criterion from std::map"
- Reply: Victor Bazarov: "Re: Erasing objects satisfying a criterion from std::map"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 10 Jan 2005 11:43:58 GMT
KaizerSose@gmail.com wrote:
> So what is the correct way of doing it?
>
There is no "correct" method. Some are more efficient,
some use less statements.
The fundamental approach is to iterate through the
map and find elements that match the criterion. Erase
the item, then start from the beginning to find the
next item. Remember that iterators are not valid after
an erase.
Another alternative is re-sort the map according to
the criterion and then erase the first item as long as
it matches. Then restore the sort order of the map.
A technique of building a view (a container of pointers
or iterators) will not be efficient since the view
must be recreated after an erase on the map (since
an erase invalidates the pointers).
The main issue is that iterators become invalid
after an erase. The elements in the container may
change positions.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
- Next message: Thomas Matthews: "Re: including a large constants file"
- Previous message: john.burton.email_at_gmail.com: "Re: Correct usage of "using" with standard templates"
- In reply to: KaizerSose_at_gmail.com: "Re: Erasing objects satisfying a criterion from std::map"
- Next in thread: Victor Bazarov: "Re: Erasing objects satisfying a criterion from std::map"
- Reply: Victor Bazarov: "Re: Erasing objects satisfying a criterion from std::map"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|