Correct use of the erase Method

Source: Internet
Author: User

Containers in STL are divided into two types by storage method: containers stored in arrays (such as vector and deque), and containers stored in discontinuous nodes (such: list, set, map ). When using functions in STL to delete elements using the erase method, you need to pay attention to some issues (I have not thoroughly understood the cause of the problem, after learning the storage form of the container, I will go back and explain the cause. The following content is only for Reading Notes ).

When you want to traverse and delete all elements in a container, or delete an element with a specific value, we often use the following method: (set is used as an example below)

#include <iostream>#include <set>using namespace std;set<int> s;set<int>::iterator it;int main(){    int i;    for (i = 0; i < 10; i++)        s.insert(i);    for (it = s.begin(); it != s.end(); it++){        if ((*it) == 5)           s.erase((*it));    }        for (it = s.begin(); it != s.end(); it++)        cout << (*it) << endl;        system("pause");}

In fact, the above method has a bug, but the compiler does not handle it. Otherwise, you should change the program to the following, and try again to run the test, and an error will occur (segment error, I have not thoroughly figured out why the above program runs out the result, and the following program will crash ):

# Include <iostream> # include <set> using namespace STD; set <int> S; set <int>: iterator it; int main () {int I; for (I = 0; I <10; I ++) s. insert (I); For (IT = S. begin (); it! = S. end (); It ++) {If (* It)> 5) // The above program mainly changed here, when you set a breakpoint for debugging, you will find that the program will first execute it ++ and then execute S. erase operation S. erase (* It);} For (IT = S. begin (); it! = S. End (); It ++) cout <(* It) <Endl; System ("pause ");}

The correct deletion method should be the following program:

#include <iostream>#include <set>using namespace std;set<int> s;set<int>::iterator it;int main(){    int i;    for (i = 0; i < 10; i++)        s.insert(i);    for (it = s.begin(); it != s.end(); ){        if ((*it) > 5)           s.erase(it++);        else it++;    }        for (it = s.begin(); it != s.end(); it++)        cout << (*it) << endl;        system("pause");}

The main reason is that the "++" operator is the opposite of our normal understanding. Erase (it ++) obtains the location of the next element and then deletes it; erase (++ it) is the location where the next element is obtained after deletion. (Here I mainly refer to some of the information above this blog: http://blog.c114.net/html/87/367587-50926.html)

However, when using a vector or deque to store containers in arrays, there is a little difference!
The procedure is as follows:

# Include <iostream> # include <vector> using namespace STD; vector <int> V; vector <int>: iterator it; int main () {int I; for (I = 0; I <10; I ++) v. push_back (I); For (IT = v. begin (); it! = V. end ();) {If (* It)> 5) It = v. erase (it); // note the modified else it ++;} For (IT = v. begin (); it! = V. End (); It ++) cout <(* It) <Endl; System ("pause ");}

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.