Re: Doesn't std::map.erase returns iterator?
On 30 mar, 00:10, "Jim Langston" <tazmas...@rocketmail.com> wrote:
Obnoxious User wrote:
On Sat, 29 Mar 2008 10:07:29 -0700, Jim Langston wrote:
[SNIP]
typedef std::map<unsigned int, CEffect*> BeamEffectsType;
// ...
for ( BeamEffectsType::iterator it = Client.BeamEffects.begin(); i=
t !=
Client.BeamEffects.end(); )
{
[SNIP]
it = Client.BeamEffects.erase(it);
[SNIP]
}
Where BeamEffects in Client is a BeamEffectsType.
[SNIP]. Is MSVC++ returning an iterator for a std::map as an extensio=
n,
Yes.
[SNIP]
Ahh, dang. I understand that in C++0x all container
erases/deletes will return an iterator,
They do now, but for some reason, they didn't in the original
version of the standard. Some library implementations went
ahead and had them return the iterator anyway (preferring common
sense, usability and good programming practice to strict
conformance), others didn't.
Just replace the line in question with:
Client.BeamEffects.erase(it);
I don't see anything in the documentation stating if .erase
invalidates iterators for a map.
It invalidates any iterators to the element which is removed,
but that's all.
I'm a little hesitant to even think about that being well
defined, however, since it would be incrementing an iterator
that would no longer be valid.
It increments the iterator before calling erase. While the
original value is still valid. It passes a copy of the original
value to erase(), but the iterator in your code already points
to the next element. (Note that a function call is a sequence
point, so the side effects of the ++ operator must have occured
before the function is called.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34