Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterator validity ,after erase() call in std::set

Tags:

c++

set

stl

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 ?
        }
    }
}
like image 558
Satbir Avatar asked Oct 28 '09 11:10

Satbir


People also ask

How do you avoid iterator invalidation in C++?

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.

Does std :: move invalidate iterators?

std::move does nothing on its own. The move assignment definitely invalidates iterators on both sides, though.

When should an iterator be treated as invalid?

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.

Is iterator valid after erase?

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.


2 Answers

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);

like image 116
Tadeusz Kopec for Ukraine Avatar answered Oct 21 '22 00:10

Tadeusz Kopec for Ukraine


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.

like image 41
peterchen Avatar answered Oct 21 '22 00:10

peterchen