Using Iterator/reverse_iterator in traversal for Erase usage

Source: Internet
Author: User
Use Iterator/reverse_iterator for Erase usage in traversal Roche (http://blog.csdn.net/kesalin/) This article follows the "signed-non-commercial-consistent" authoring common agreement

It is well known that when iterating through an STL container using an iterator, special attention needs to be paid to whether the iterator is modified in the loop and causes the iterator to fail. Let me conclude by summarizing the use of iterators correctly when deleting elements in forward and backward traversal of various containers. This article source code: https://code.csdn.net/snippets/173595

First, it's a good idea to understand that using forward iterators (iterator) for backward traversal is a bad use, or why you have a reverse iterator (reverse_iterator). Secondly, depending on the characteristics of the container, the use of traversal deletes can be divided into two groups, the first group is list and vector, and the second group is map and set.


Next, look at how the specific usage.

First Scenario: Forward traversal Delete element

For lists and vectors, their erase function returns the next iterator, so that when traversing, only it = c.erase (it) is required; Can.

For map and set, their erase function returns void, and after erase, the current iterator is invalidated and cannot be used to get the next iterator. So before you need to erase, you get an iterator that points to the next element. Such as:

Tmpit = it;
++it;
C.erase (Tmpit);
The three lines of code on the suffix + + operator (first creating a replica, then incrementing the iterator, and then returning a copy) can be reduced to one line:
C.erase (it++);


List forward Traversal delete element sample (same vector usage ):

    List<int>::iterator it;
    for (it = L.begin (); it!= l.end ();)
    {
        if (0 = (*it)% 2) {
            it = l.erase (it);
        }
        else {
            ++it;
        }
    }

Map Forward Traversal delete element sample (same set usage):

    Map<int, int>::iterator mit;
    for (mit = M.begin (); MIT!= m.end ();)
    {
        if (0 = = Mit->first% 2) {
            m.erase (mit++);
        }
        else {
            ++mit;
        }
    }

in the second case, the reverse traversal deletes the element

On the relationship between forward/reverse iterators, refer to the effective STL, here I only point out that the difference between the two elements, from a reverse iterator to obtain the corresponding forward iterator needs to use the base () method. As the following illustration shows: Ri is a reverse iterator that points to element 3, and I is the ri.base () of the desired iterator.


Since all erase functions only accept forward iterators iterator, the reverse traversal of the deletion element requires that the reverse_iterator be converted to iterator first, and then the problem of updating the iterator is considered.

First, you will analyze how to convert reverse_iterator to iterator. As shown in the figure above, we want to delete element 3, and the forward iterator I from ri.base () points to actually 4, so in order to delete element 3 correctly, you need to move the RI forward (reverse) one position. In other words, the deletion usage for this step should be:

C.erase ((++rit). Base ());

Or: (think about why.) , but this usage is not portable because some STL implementations do not allow you to modify the pointer returned by the function.

C.erase (Rit.base ();


Then, we'll analyze the problem with the iterator update.
For List/vector, because the erase can return a valid forward iterator, it is only necessary to convert the returned forward iterator to a reverse iterator.

For Map/set, the iterator has been updated since the deletion operation L.erase (++rit). Base (). It can also be seen from here that the code is clearer using this first increment base () conversion deletion method.

Now that the theoretical analysis is complete, let's look at the concrete examples below.

List Reverse Traversal delete element sample (same vector usage):

    Erase with Reverse_iterator
    list<int>::reverse_iterator RIT;
    for (RIT = L.rbegin (); RIT!= l.rend ();)
    {
        if (0 = (*rit)% 2) {
            RIT = List<int>::reverse_iterator (l.er ASE ((++rit). Base ());
        }
        else {
            ++rit;
        }
    }

Map Reverse Traversal Delete element sample (same set usage):

    Erase with Reverse_iterator
    Map<int, Int>::reverse_iterator RIT;
    for (RIT = M.rbegin (); RIT!= m.rend ();)
    {
        if (0 = rit->first% 2) {
            m.erase ((++rit). Base ());
        else {
            ++rit;
        }
    }

OK, delete Usage I'm sure we all understand, but, however, the operation that causes the iterator to fail is also insert operation Ah, believe that smart you must be able to extrapolate correctly update iterators ~ ~


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.