Re: Doesn't std::map.erase returns iterator?
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(); it !=
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 extension,
Yes.
[SNIP]
Ahh, dang. I understand that in C++0x all container erases/deletes will
return an iterator, but that doesn't help me now. It means I'm going to
have to come up with a new algorithm for one I found quite useful and was
content with.
I don't see anything in the documentation stating if .erase invalidates
iterators for a map. My guessing is it doesn't, allowing me to use:
for ( BeamEffectsType::iterator it = Client.BeamEffects.begin(); it !=
Client.BeamEffects.end(); )
{
Client.BeamEffects.erase(it++);
}
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.
If that is not valid I would have to do something like:
for ( BeamEffectsType::iterator it = Client.BeamEffects.begin(); it !=
Client.BeamEffects.end(); )
{
BeamEffectsType::iterator sit = it + 1;
Client.BeamEffects.erase(it);
it = sit;
}
although I might have to use .advance or something not sure if map iterators
have + overriden.
What is a normal algorithm for erasing some entries in a map while iterating
over them? Even if using an index size_t and a for loop I'm going to have
to go thorugh shenanigans.
--
Jim Langston
tazmaster@rocketmail.com