The problem of pointer failure after insert/erase in vector

Source: Internet
Author: User

Before looking at "C + + Primier", also solved in the sequential window insert/erase will involve the problem of iterator invalidation, and did not delve into. I had this problem writing a program today.

1 Beware of erase in Ji-ji

Originally my program is Jiangzi, don't say, I know this is problematic, but this is the most intuitive idea

        vector<int> A;      for (int0; + +i)    {        a.push_back (i);    }      for (Auto it = A.begin (); It! = A.end (); + +it        ) {if5) {            a.erase (it);        }    }   

Yes, the program crashes! After the iterator it has been removed, the IT iterator is invalidated and the ++it operation is no longer possible.

Looked up the book and found that the previous iterator to it was valid. So, wit like me:

    vector<int> A;      for (int0; + +i)    {        a.push_back (i);    }      for (Auto it = A.begin (); It! = A.end (); + +it        ) {if5)            {= it;            --it;            A.erase (IT1);        }    }    

blingbling! Wit not wit? I asked you, wit is not witty? Before I deleted 5, I had it point to 4, so after removing 5, ++it made it point to 6, the new iterator!

2 more careful, Ji-ji Insert

Wit, like me, will naturally explore what happens to an iterator after the insert. So:

    vector<int> A;      for (int0; + +i)    {        a.push_back (i);    }      for (Auto it = A.begin (); It! = A.end (); + +it        ) {if5)             {100 );
++it; } }

You know what??

What's not! You may ask why you should ++it after inserting. Before inserting, it points to 5, after inserting 100 before 5, it points to 100. So the next cycle, it will still point to 5. Trust me, your program will explode!

After I made a ++it, it also points to 5, and the next loop points directly to the element after 5, which completes the insertion process successfully.

World Peace ~ World Peace ~ I'm not so sure.

It suddenly occurred to me that when the insertion element was too large, the capacity of the vector would increase, wouldn't it be a problem? Just do what you say:

    vector<int> A;      for (int0; + +i)    {        a.push_back (i);    }      for (Auto it = A.begin (); It! = A.end (); + +it        ) {if5)             {100     ); + +it;        }    }  

Why is it 13? Because I debug, vs tells me the capacity of the vector.

boom! It's crashing! This means that the iterator after the insertion is invalid. What about before that?

I decided to test it rudely:

    vector<int> A;      for (int0; + +i)    {        a.push_back (i);    }      for (Auto it = A.begin (); It! = A.end (); + +it        ) {if5)             {100  );  it=a.begin ();        }    }  

After I insert it, direct it to begin (), and then step into the debugging. After executing it=a.begin () also good, can go to perform ++it or collapse.

In other words, after the capacity changes, all iterators are invalidated! Too scary. I dare not conclude.

Increasingly feel the need to see the "STL Source Analysis" ...

The problem of pointer failure after insert/erase in vector

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.