Iterator failure-erase () function trap for container operation in STL __stl

Source: Internet
Author: User

The iterator invalidation occurs when we use the container to delete a container node through an iterator to remove the iterator from being invalidated. We can find answers from the iterators used in each container and the structure of the container itself, and there is no need to memorize those traps and correct usage.

The type of iterator used for each container can be seen in this blog, so let's take a look at the iterator failures of each container separately.

1.vector and Deque

Both of these containers support random access iterators, can be random to the point that can be arbitrarily through various steps forward or to later access to the container node, support the random access iterator is also because the nodes of the two containers themselves are composed of the address order, and therefore can be based on random steps to access each node. Since vectors and deque are all in order in composition, it appears that when we delete the current node through the iterator, it will result in the next node being accessed again, the memory that the current iterator is pointing to is not available, so it can cause invalidation. The reason for this is that the sequential structure will move the nodes in the back when the node is deleted. In particular, the internal implementation of the container will transfer this sequence of nodes to other memory addresses at the time of expansion.

The following is the correct method for node deletion using the Earse () function: (Erase will return the correct address of the node following the delete node to the iterator variable)

std::vector< int> Vec;
std::vector< int>::iterator Itvec;
for (Itvec = Vec.begin (); Itvec!= vec.end ();) {
      if (*itvec) {
          Itvec = vec.erase (Itvec);
       }
      else{
          itlist++
       }
}
2.list Set and map

List already set and map the associative container has a common structure that they all combine through not necessarily continuous node structures, and each node is connected by saving each other's addresses, so the iterator type they use is a bidirectional iterator. This iterator will only invalidate the current node address when it deletes the current node, without invalidating the address of the node that is behind it. Because they do not need to be rewritten in each node, we can still find the address of the next node based on the current deletion node iterator. The erase () function here also returns the address of the next node.

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++
      }

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




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.