The container calls its own erase method. What should I do next?

Source: Internet
Author: User

When I wrote a small example today, I encountered the following problem: I want to traverse a vector, but when some conditions in the traversal process are met, I plan to delete the elements of the composite condition. Use erase to permanently delete the file when deleting the file, and then continue to traverse the file. (in this case, I have seen it on Objective C ++, but I cannot find it. I still remember there is a solution, so you can find a solution ). For example, I want to delete 2 directly when traversing 2. The deleted array can also be used as it.

 

Int B [] = {1, 2, 4, 5 };

Vector <int> A (B, B + 5 );

For (vector <int >:: iterator I = B. Begin (); I! = B. End (); I ++)
{

If (* I = 2)

{
B. Erase (I );

}
Cout <* I;
}

In this way, only 1 is output, and then the access is illegal. The reason is that after erase deletes the element to which the iterator points, the iterator is no longer valid.

Next, we will consider whether to skip this output using continue, starting from the next time:

For (vector <int >:: iterator I = B. Begin (); I! = B. End (); I ++)
{

If (* I = 2)

{
B. Erase (I );

Continue;

}
Cout <* I;
}

This is also not possible because continue only skips the part of the loop that has not been completed. However, I ++ cannot be blocked. I ++ causes the problem. Because I ++ uses an expired old deleted iterator. To avoid such errors, update the iterator. Because erase automatically returns the next iterator of the deleted iterator. So the new program is as follows:

For (vector <int >:: iterator I = B. Begin (); I! = B. End (); I ++)
{
If (* I = 2)
{
I = B. Erase (I );
}
Else
Cout <* I;
}

At this time, the output is: 145. Where does 3 go? In case of 2, the iterator pointing to 2 is deleted, and the iterator is updated to point to 3. However, I ++ is executed next, so that the judgment and use of 3 is skipped. That is, 3 is blocked.

From the above we can see that I ++ is an inevitable source of problems. So now we want to make I ++ controllable so that it can be implemented inside the loop body, that is, it can be implemented using the while loop.

Vector <int>: iterator I = B. Begin ();
While (I! = B. End ())
{
If (* I = 2)
{
I = B. Erase (I );
}
Cout <* I <"";
I ++;
}

In this way, the goal can be achieved. The output is as follows:

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.