Precautions for deleting elements in Stl

Source: Internet
Author: User
Containers in STL are divided into two types by storage method: containers stored in arrays (such as vector and deque), and containers stored in discontinuous nodes (such: list, set, map ). When using the erase method to delete elements, pay attention to some issues.
When using list, set, or map traversal to delete some elements, you can use the following method:

Method 1

Std: list <int> List;
Std: list <int >:: iterator itList;
For (itList = List. begin (); itList! = List. end ();)
{
If (WillDelete (* itList ))
{
ItList = List. erase (itList );
}
Else
ItList ++;
}

Or

Correct method 2

Std: list <int> List;
Std: list <int >:: iterator itList;
For (itList = List. begin (); itList! = List. end ();)
{
If (WillDelete (* itList ))
{
List. erase (itList ++ );
}
Else
ItList ++;
}


The following are two incorrect usage methods:

Error Method 1

Std: list <int> List;
Std: list <int >:: iterator itList;
For (itList = List. begin (); itList! = List. end (); itList ++)
{
If (WillDelete (* itList ))
{
List. erase (itList );
}
}

Or

Error Method 2

Std: list <int> List;
Std: list <int >:: iterator itList;
For (itList = List. begin (); itList! = List. end ();)
{
If (WillDelete (* itList ))
{
ItList = List. erase (++ itList );
}
Else
ItList ++;
}

Correct Method 1: Obtain the location of the next element through the returned values of the erase Method
Correct Method 2: Use "++" to obtain the location of the next element before calling the erase method.
Incorrect Method 1: After the erase method is called, "++" is used to obtain the location of the next element. Because the element location has been deleted after the erase method is called, if you obtain the next location based on the old location, an exception occurs.
Error Method 2: Same as above.

Here, the "++" operator is the opposite of our normal understanding. erase (itList ++) first obtains the location of the next element and deletes it; erase (++ itList) is the location of the next element after deletion.

When you use vector and deque to traverse and delete elements, you can also obtain the position of the next element through the returned values of erase:

Correct usage

Std: vector <int> Vec;
Std: vector <int >:: iterator itVec;
For (itVec = Vec. begin (); itVec! = Vec. end ();)
{
If (WillDelete (* itVec ))
{
ItVec = Vec. erase (itVec );
}
Else
ItList ++;
}


Note: vector and deque cannot be traversed and deleted in the "correct method 2" method above.

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.