C + + List erase ()

Source: Internet
Author: User

The containers in the STL are stored in two categories, one for containers that are stored as arrays (e.g. vectors, deque), and those that are stored in discontinuous nodes (for example, list, set, map). There are some issues to be aware of when using the Erase method to remove elements.
You can use this when you use list, set, or map traversal to delete certain elements:
correct use of 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++;
      }

Correct use 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++;
}

here are two ways to use the error:

std::list< int> list;
std::list< Int>::iterator itlist;
for (itlist = List.begin (); Itlist! = List.end (); itlist++)
{
if (Willdelete (*itlist))
{
List.erase (itlist);
}
}

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 use Method 1: Get the position of the next element by the return value of the Erase method
Method 2: Use "+ +" to get the position of the next element before calling the Erase method
Error using Method 1: Use "+ +" after calling the erase method to get the position of the next element, because the position of the element has been deleted after the erase method is called, and an exception occurs if the next position is obtained from this old location.
Error using Method 2: Ibid.

The "+ +" operator here is exactly the opposite of what we normally understand, erase (itlist++) is the position at which the next element is first fetched, and erase (++itlist) is the position at which the next element is deleted.

C + + List erase ()

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.