Element deletion traps in STL containers

Source: Internet
Author: User

Today, I read the usage of STL by Scott Meyers and saw a mistake I made some time ago. I found that the code I wrote is almost the same as the error code he mentioned, the error code is as follows:
STD: vector <struct> mfriendlist;
...
STD: vector <struct >:: iterator iter = mfriendlist. Begin ();
For (; iter! = Mfriendlist. End (); ++ ITER)
{
If (...)
Mfriendlist. Erase (ITER );
}
I remember once I told me this problem and changed the code. I didn't understand why, but I only knew that when the program was executed, if it was true, the program would definitely crash.
The master statement is: When an easy element is deleted, all iterators pointing to the element become invalid. In the above Code, as long as the erase (ITER) is executed, the ITER will become invalid, and the execution of ++ ITER will certainly fail.

On the Internet, we can see two summary items:
1. for deletion of node-type containers (MAP, list, set) elements, the insert operation will invalidate the iterator pointing to this element, and the iterators of other elements will not be affected.
2. Deletion and insertion of the vector, string, and deque elements in a sequential container will invalidate the iterator pointing to this element and subsequent elements.

I summarized it and thought back to the code I modified at the time, so the correct method should be as follows:
1. For node-type containers
STD: List <struct> MList;
...
STD: List <struct >:: iterator iter = MList. Begin ();
For (; iter! = MList. End ();)
{
If (...)
{
// Because the node type will only cause the current node iterator to become invalid, the iterator will be moved back when the node is deleted, because other elements will not become invalid.
MList. Erase (ITER ++ );
}
Else
{
++ ITER;
}
}

2. For sequential containers
STD: vector <struct> mvector;
...
STD: vector <struct >:: iterator iter = mvector. Begin ();
For (; iter! = Mvector. End ();)
{
If (...)
{
// Here we have a saying, because the sequential container will make itself and the subsequent element iterator invalid, so it cannot be a simple ++ operation.
// Here, the erase () of the sequential container will return a valid iterator for the next element following the deleted element.
// The erase () returned value of the node-type container is void, which is amazing and amazing !!!!
Iter = mvector. Erase (ITER );
}
Else
{
++ ITER;
}
}

NOTE: The container depends on the implementation of the STL library. earse of the two types of containers in vs returns the next iterator pointer.

Element deletion traps in STL containers

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.