Vector Delete, erase and remove no wonder--"STL"

Source: Internet
Author: User

For vectors to use containers. Usually just a simple traversal lookup, other operations have been performed, which is not, today, slightly careless.

The action of the erase method is to delete the node at this point, and then point to the next node that is deleted:

As to Data 1 6 6 4 7;

#include <iostream> #include <vector> #include <algorithm>using namespace Std;int main () {    vector <int> Vec;    Vec.push_back (1);    Vec.push_back (6);    Vec.push_back (6);    Vec.push_back (4);    Vec.push_back (7);    Vector<int>::iterator arr;    Vec.erase (Remove (Vec.begin (), Vec.end (), 6), Vec.end ());    for (arr = Vec.begin (); Arr! = Vec.end (); arr++)    {        if (6 = = *arr)        {            vec.erase (arr);            arr--;        }    }    cout << "The size of vector is:" << vec.size () << Endl;    for (arr = Vec.begin (); Arr! = Vec.end (); arr++)    {        cout << *arr << "  ";    }    cout << Endl;    return 0;}
The results of the above actions are as follows:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvegptmtk5/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70 /gravity/southeast ">

One 6 was not successfully deleted because the iterator was arr++after each loop, and after the node was successfully deleted, a bit was moved. In fact, when the deletion is done, it moves back two bits, so when it is deleted:

for (arr = Vec.begin (); Arr! = Vec.end (); arr++)    {        if (6 = = *arr)        {            vec.erase (arr);            arr--;        }    }
with the arr-- operation, you can offset your own active back-shift when deleting. Thus successfully:


Of course, erase has another way of removing all the elements between two nodes. This relies on the remove operation to move all nodes that are equal to the deleted value to the end of the container. To delete;

The gaze of the above for loop is removed by the vec.erase (remove (Vec.begin (), Vec.end (), 6), Vec.end ()) on the previous line; Gaze cancellation can be achieved;

#include <iostream> #include <vector> #include <algorithm>using namespace Std;int main () {    vector <int> Vec;    Vec.push_back (1);    Vec.push_back (6);    Vec.push_back (6);    Vec.push_back (4);    Vec.push_back (7);    Vector<int>::iterator arr;    Vec.erase (Remove (Vec.begin (), Vec.end (), 6), Vec.end ());    /*for (arr = Vec.begin (); Arr! = Vec.end (); arr++    ) {        if (6 = = *arr)        {            vec.erase (arr);            arr--;        }    } *    /cout << "The size of vector is:" << vec.size () << Endl;    for (arr = Vec.begin (); Arr! = Vec.end (); arr++)    {        cout << *arr << "  ";    }    cout << Endl;    return 0;}

O (∩_∩) o

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Vector Delete, erase and remove no wonder--"STL"

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.