Use erase () in STL with caution

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_67b6b720010114d3.html

The erase () function is used to delete elements in a container.
Delete an element in a container: C. Erase (t );
It seems like a simple action. However, different types of containers are completely different internally, which will be introduced later.

Suppose there is a question that deletes all elements in a container that meet the condition n = x. According to the general idea, there should be code similar to this:

//Assume that container and container represent a container and an object respectively.
Container <t >:: iteratorIt;
For(It=Container. Begin ();It! =Container. End ();++ It){
  If(N=X)
    Container. Erase (it );
}

However, such code is wrong for any container.

Containers can be divided into linked list containers and array containers by memory allocation.

A linked list container is a node-based container, such as list and slist (dynamically allocating memory blocks) it is associated with containers such as set, MAP, Multiset, and multimap (implementation of the Balance Tree), while the array container refers to a continuous memory container that saves elements on a continuous memory, such as vector, deque, and string.

The linked list container uses list as an example. erase (IT) Does Delete the first element that meets the condition, but the IT pointer has been deleted and it does not point to any element, therefore, the code above can only be used to delete the first element that meets the conditions correctly for the linked list container. To solve this problem, we first think that before deleting the pointer, back up the data.

This temporary variable is directly built in the erase implementation, which is more concise and professional.

List <int >:: iteratorIt;
  For(It=Lt. Begin ();It! =Lt. End ();){
    If(* It%2=0)
      Lt. Erase (It++); //Here is the key
    Else
      ++ It;
  }

 

Another feature of using erase to delete nodes in linked list containers is that the address of the next element is returned, so you can also achieve this:

List <int >:: iteratorIt;
  For(It=Lt. Begin ();It! =Lt. End ();){
    If(* It%2=0)
      It=Lt. Erase (it); // automatically returns the address of the next element, no need to take the initiative to forward the pointer
    Else
      ++ It;
  }

 

The array container uses vector as an example. in erase (it), as mentioned above, the first element meeting the condition is deleted, but in this case, the array container does not allow "gaps" in the middle, so it will take a big action, it is to move forward all elements after the deleted element (refer to STL source code), andThe array container records subscriptTherefore, after the element is deleted, the element to be located in the current subscript is naturally changed to the next element in the original queue. The Code is as follows:

Vector <int>: iteratorIt=V. Begin ();
  For(It=V. Begin ();It! =V. End ();){
    If(* It%2=0)
      V. Erase (it); // After the element is deleted, the subsequent elements are automatically moved forward without moving 
    Else
      ++ It;
  }
 It is said on the Internet that the V. Erase (IT) method above in vs2005 is good.Errors may occur when running vs2008 and 2010.
Vector erase iterator outside rangeThe safest way isSetChange v. Erase (IT)It =V. Erase (it)

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.