Vector Erase () function analysis __ function

Source: Internet
Author: User

The

Erase function can be used to delete one or more elements of a vector container. When you delete an element, its argument is an iterator that points to the corresponding element, and when you delete an element, the argument is an iterator that points to the beginning of the element and an iterator that points to the next element of the end element:
After a single element is removed, the incoming iterator points unchanged, still points to the location of the deleted element, and all elements after the deleted element move forward one bit, that is, the iterator actually points to the next element of the original deleted element.
When you delete an element, the incoming iterator points to the same point, still pointing to where it was originally passed, and modifying the position of the element that follows the segment.
test its memory changes with the following code:

Test the change in the incoming iterator parameters for the vector in which the iterator uses the erase function.

#include <iostream> #include <vector> using namespace std;
    int main (void) {vector<int>array;
    Array.push_back (100);
    Array.push_back (300);
    Array.push_back (300);
    Array.push_back (500);
    Array.push_back (600);
    Vector<int>::iterator Itor;
            Deletes a single element for (Itor = Array.begin (); Itor!= array.end (); itor++) {if (*itor = 300) {
        Itor = Array.erase (itor);
    for (Itor = Array.begin (); Itor!= array.end (); itor++) {cout << *itor << "";
    } cout << Endl;
    Delete a section of the element vector<int>::const_iterator Iter_begin = Array.begin () + 1;
    Vector<int>::const_iterator iter_end = Array.end ()-1;
    Array.erase (Iter_begin, iter_end);
    for (Itor = Array.begin (); Itor!= array.end (); itor++) {cout << *itor << ""; }//Delete last element vector<int>::iterator Iter_last = ARRAY.End ()-1;
    Array.erase (Iter_last);
    for (Itor = Array.begin (); Itor!= array.end (); itor++) {cout << *itor << "";
return 0; }

value in memory before deleting a single element of 300:
That is, the elements in the container are 100, 300, 300, 500, 600

Remove the iterator that points to the element before 300:

Remove changes in memory values after 300:
The elements in this container are: 100, 300, 500, 600

After you delete 300, the iterator condition:

By the contrast above, before and after deleting a single element, the iterator's point is invariant, and after the element is deleted, the elements behind the deleted element will all copy the position of the element forward, pointing to the next iterator on the end of the container, that is, Array.end () will also move forward one bit, That is, the end turns into a red 600, and array.end () points to the white 600.

To delete a section of an element:
The memory before the deletion is:
The elements in the container are: 100, 300, 500, 600

Suppose that you want to delete 300 and 500;
The incoming arguments should be two iterators pointing to the next digit of 300 and 600 (500);

After the delete operation is performed, the container memory condition is:
The elements in the container after deletion are: 100, 600.

After the delete operation is performed, the case for the two iterators as parameters is:

As you can see from the picture above, after you delete a section of elements, the following elements (from Iter_end to Vector.end ()) are copied to the beginning of the deleted element segment (Iter_begin), and Vector.end () also moves forward based on the number of elements that are deleted. The figure above is moved to 500, the next position in the last element 600.

Summary: The delete operation passes in the iterator, and the position pointed to by the iterator does not change before and after the deletion, change is only the element value in the container, after the deletion of the element, all elements behind the deleted element are copied to the location of the deleted element, while the iterator pointing to the end of the container is also moved to the new trailing position.

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.