"C # Lazy Egg programming-5 minutes experience sharing" 01 reverse traversal Delete to prevent iterator invalidation

Source: Internet
Author: User
Because the space is very short, try writing it all. It's not necessary to finish the text in a few minutes or more than 10 minutes.

The Foreach Traversal list operation is convenient, saving some [] code for data and subscript calculations. It's simple to write. However, if you are traversing delete, you will encounter a classic iterator invalidation problem.

A common idea to solve this problem is to put a temporary container B on the element that needs to be removed in container a. Then traverse this container b again, remove the element and delete it in a. This is a lot of shortcomings, two cycles, and adding temporary containers, the code is very difficult to see.

In fact, the simplest way to prevent (or avoid encountering) the failure of an iterator is to delete it while traversing the delete. This writing is classic, that is i--some people seem not accustomed to it.

"C # Lazy Egg programming-5 minutes experience sharing" 01 reverse traversal Delete to prevent iterator invalidation

Related Article

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.