A detailed explanation of how to delete a specific value of a vector and the error occurs.

Source: Internet
Author: User

 

With regard to erase, I understand that: Delete the specified element, and then move all the elements behind the deleted element forward. then change the end pointer. move one forward. most importantly, the current iterator does not move (but because the element moves forward, it points to the next element in the original state ).

For example, I have a vector where the elements of the vector are 0, 1, 2, 3, and 4. If I want to delete the elements whose element value is 3. when I use erase, its memory elements are 0 (BEGIN), 1, 2, 4 (end), 4. after the iterator is deleted, it points to 4 elements. (The first 4)

It looks cool. Based on this principle, let's look at a few simple examples.

First, it is a problem most easily overlooked by beginners:

Vector <int> vectint;

Int I;

For (I = 0; I <5; I ++)

{

Vectint. push_back (I );

}

// The following code deletes all elements with a value of 4:

Vector <int>: iterator itvect = vectint. Begin ();

For (; itvect! = Vectint. End (); ++ itvect)

{

If (* itvect = 4)

{

Vectint. Erase (itvect );

}

}

This section of code is generally written by beginners. It mainly involves the issue of iteration failure. (This is just a brief description of the Error.) In fact, it is a big problem and I will not discuss it for the time being.

Let's look at another example:

Vector <int> vectint;

Int I;

// Initialize the vector container

For (I = 0; I <5; I ++)

{

Vectint. push_back (I );

If (3 = I)

{

Vectint. push_back (I );

}

}

Vector <int>: iterator itvect = vectint. Begin ();

Vector <int>: iterator itvectend = vectint. End ();

// The following code deletes all elements with a value of 3:

For (; itvect! = Itvectend; ++ itvect)

{

If (* itvect = 3)

{

Itvect = vectint. Erase (itvect );

}

}

// Here is the output Element

Int isize = vectint. Size ();

For (I = 0; I <isize; I ++)

{

Cout <"I =" <I <"," <vectint [I] <Endl;

}

Getchar ();

Here we can simply debug the program. when an element value of 3 is deleted, the whole vector following it moves forward to a unit. therefore, an element will be deleted.

Output result:

I replace the output section with the code to find out the implementation principle:

Int isize = vectint. Size ();

For (I = 0; I <isize + 2; I ++)

{

Cout <"I =" <I <"," <vectint [I] <Endl;

}

Getchar ();

The output result is:

There is another problem:

Why does the size of the entire vector not change after I delete the specified number?

That is to say, I used to have 0 ~ Five elements. Why didn't I unregister the last memory block after I deleted one element?

According to my understanding, when you generate a vector object, it is not that you apply for pushback once, but that you initialize a memory block first. when the size of the region you want to store is larger than the initial size, it will apply for an incremental space for you to use again. therefore, I don't think it is necessary for the system to clear the memory of the elements that are moved later (because it is occupied by memory). It simply moves the end pointer. in this way, when you add an element next time, it will overwrite it.

Another point is to pay attention to the storage method of vector: sequential storage.

After carefully understanding the two simple examples above, it is not difficult to write the code to delete a specific group of elements:

Void deleteitem (vector <int> & vecint, int deletevalue)

{

For (vector <string >:: iterator iter = vecint. Begin (); iter! = Vecint. End ();) // The end pointer cannot be saved and used.

{

If (* iter = deletevalue)

{

Vecint. Erase (ITER );

Continue;

}

ITER ++;

}

Return;

}

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.