How C + + iterates over deleting elements inside Map/vector

Source: Internet
Author: User

New Skill get!

Problem

For containers inside C + +, we can use iterator for easy traversal. But when we make changes through iterator to Vector/map and so on, we need to be careful, because operations often lead to iterator failure, and subsequent behavior becomes unpredictable. Like what:

#include <iostream> #include <vector>using namespace Std;int main () {    vector<int> a = {12, 23, 34, 45        ,---------- for (Auto iter = A.begin (); ITER! = A.end (); ++iter) {        if (*iter >) {            a.erase (ITER);        }    }        for (const auto &element:a) {        cout<<element<<endl;    }        return 0;} Output: 1223456789

The description of std::vector::erase in Cplusplus's reference is:

Iterators, pointers and references pointing to position (or first) and beyond is invalidated, with all Iterators, pointers and references to elements before position (or first) is guaranteed to keep Referri Ng to the same elements they were referring to before the call.

Only the iterator in front of the deleted element remains valid, and subsequent traversal behavior is unpredictable.

Solution Solutions

For vectors, Erase will return to the next iterator, so we can use the following method:

#include <iostream> #include <vector>using namespace Std;int main () {    vector<int> a = {12, 23, 34, 45        ,---------- Auto iter = A.begin ();    while (iter! = A.end ()) {        if (*iter >) {            iter = a.erase (ITER);        }        else {            ++iter;        }    }        for (const auto &element:a) {        cout<<element<<endl;    }        return 0;} Output: 1223

  

For map, deleting iterator only affects the current iterator, so using a for loop is sufficient, such as:

#include <iostream> #include <map>using namespace Std;int main () {    Map<int, int> a = {{1, 12}, {2, 23} , {3, (+), {4, +}, {5, 6, +}};        for (Auto iter = A.begin (); ITER! = A.end (); ++iter) {        if (Iter->second >) {            a.erase (ITER);        }    } For        (const auto &element:a) {        cout<<element.first<< ":" <<element.second<<endl;    }        return 0;} Output: 1:122:23

But it's more recommended to have iterator point to the next element before erase.

#include <iostream> #include <map>using namespace Std;int main () {    Map<int, int> a = {{1, 12}, {2, 23} , {3, (+), {4, +}, {5, 6, +}};        Auto iter = A.begin ();    while (iter! = A.end ()) {        if (Iter->second >) {            a.erase (iter++);        }        else {            ++iter;        }    }        for (const auto &element:a) {        cout<<element.first<< ":" <<element.second<<endl;    }        return 0;} Output: 1:122:23

  

Resources

Http://stackoverflow.com/questions/4645705/vector-erase-iterator

Http://stackoverflow.com/questions/4600567/how-can-i-delete-elements-of-a-stdmap-with-an-iterator

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.