Do erase call in std::set invalidate iterator ? As i have done below 5th from last line..? if yes what is better way to erase all elements from set
class classA
{
public:
classA(){};
~classA(){};
};
struct structB
{
};
typedef std::set <classA*, structB> SETTYPE;
typedef std::map <int, SETTYPE>MAPTYPE;
int __cdecl wmain (int argc, wchar_t* pArgs[])
{
MAPTYPE mapObj;
/*
...
.. Some Operation Here
...
*/
for (MAPTYPE::iterator itr1=mapObj.begin(); itr1!=mapObj.end(); itr1++)
{
SETTYPE li=(*itr1).second;
for (SETTYPE::iterator itr2=li.begin();itr2!=li.end();itr2++)
{
classA *lt=(classA*)(*itr2);
li.erase(itr2);
delete lt; // Does it invalidate Iterator ?
}
}
}
To avoid invalidation of references to elements you can use a std::deque if you do not insert or erase in the middle. To avoid invalidation of iterators you can use a std::list.
std::move does nothing on its own. The move assignment definitely invalidates iterators on both sides, though.
Iterator Invalidation in C++When the container to which an Iterator points changes shape internally, i.e. when elements are moved from one position to another, and the initial iterator still points to the old invalid location, then it is called Iterator invalidation.
References and iterators to the erased elements are invalidated. Other iterators and references are not invalidated. This means that once the item is erased, the iterators that pointed to it are no longer valid.
From standard 23.1.2
The insert members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements.
EDIT
In your case itr2 is invalidated after erasing so incrementing it causes undefined behaviour. In this case you can follow reko_t advice, in general, you can try this:
for (SETTYPE::iterator itr2=li.begin();itr2!=li.end();)
{
classA *lt=(classA*)(*itr2);
li.erase(itr2++);
delete lt;
}
which will increment iterator before removing it's previous value from set.
BTW. itr2 is not invalidated by delete lt;
, but by li.erase(itr2);
The delete is ok.
The problem is that you erase - and thus invalidate - itr2
, but use it for loop iteration.
i.a.w. after the first erase, the ++itr2
has undefined results.
The pattern I use in this situation is this:
while(itr2 != end())
{
iterator toDelete = itr2;
++itr2; // increment before erasing!
container.erase(toDelete);
}
Some non-standard STL impls have erase return the next iterator, so you could do:
while(itr2 != end())
itr2 = container.erase();
that's not portable, though.
the set<A*,B>
is strange, though - in a standard impl, B would be the comparator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With