The STL selects the method to delete elements with caution, and the stl selects the elements with caution.

Source: Internet
Author: User

The STL selects the method to delete elements with caution, and the stl selects the elements with caution.
Zookeeper

1. Delete all objects with specific values in the container

1. If the container is a vector, string, or deque, use erase-remove. For example:
Vector <int> c;

C. erase (remove (c. begin (), c. end (), 1963), c. end (); // delete an element whose value is 1963

The following describes the algorithm remove:

Template <classForwardIterator, class T>

ForwardIteratorremove (ForwardIterator first, ForwardIterator last, const T & value)

{

First = find (first, last, value );

ForwardIterator next = first;

Return first = last? First: remove_copy (++ next, last, first, value );

}

Template <classInputIterator, class OutputIterator, class T>

OutputIteratorremove_copy (InputIterator first, InputIterator last, OutputIterator result, constT & value)

{

For (; first! = Last; ++ first)

If (* first! = Value)

{

* Result = * first;

++ Result;

}

Return result;

}

Remove all elements equal to value in [first, last. This algorithm does not really delete those elements from the container (in other words, the container size is changed), but does not equal each element to the value (that is, we do not intend to remove it) the element is assigned to the space after first. The return value indicates the next position of the last elements after the reorganization. For example, if we execute remove () to remove all 0-value elements, the execution result will be }. Each element that is not equal to 0 is copied to the first, second, third, and fourth positions, respectively. The fourth position does not move after, in other words, the fourth position is followed by the residual data left by this algorithm. The returned value ForwardIterator points to the fifth position. If you want to delete the residual data, you can hand over the returned iterator to the erase () member function of the container where the range is located. Note: array is not suitable for remove () and remove_if (), because the array cannot be resized, resulting in permanent residual data. For array, the popular algorithms are remove_copy () and remove_copy_if ().

Remove_copy removes all elements that are equal to the value in the [first, last) range. It does not really delete those elements from the container, but copies the results to a container with the starting position indicated by the result. The new container can overlap with the original container, but if the actual value of the new container exceeds the size of the old container, unexpected results will be generated. The returned value OutputIterator indicates the next position of the last copied element.

2. If the container is list, use list: remove. For example:

List <int> c;

C. remove (1963); // This member function has no return value.

3. If the container is a standard associated container, use its erase member function. For example:

Map <int, int> c;

C. erase (1963); // delete an element whose key value is 1963

It is totally wrong to use any action named remove for the standard associated container. Such a container does not have a member function named "remove". Using the remove algorithm may overwrite the container value and damage the container.

2. Delete all objects in the container that meet specific criteria

1. If the container is a vector, string, or deque, use erase-remove_if for usage. For example, we will not delete all elements that are equal to a specific value from c, but delete each object that returns true in the following discriminant:

Bool badvalue (int );

C. erase (remove_if (c. begin (), c. end (), badvalue), c. end ());

2. For list, use list: remove_if. For example:

C. remove_if (badvalue );

3. If the container is a standard associated container, use remove_copy_if and swap, or write a loop to traverse the elements in the container. Remember that when the iterator is passed to erase, to increase the suffix. For example:

AssocContainer <int> c;

...

AssocContainer <int> goodvalues;

Remove_copy_if (c. begin (), c. end (), inserter (goodvalues, goodvalues. end (), badvalue );

C. swap (goodvalues );

Or

AssocContainer <int> c;

...

For (AssocContainer <int>: iteratori = c. begin (); I! = C. end ();)

{

If (badvalue (* I ))

C. erase (I ++ );

Else

++ I;

}

3. Perform some (except deleting objects) operations within the loop:

1. If the container is a standard sequence container, write a loop to traverse the elements in the container. Remember to update the iterator with its return value every time erase is called. For example:
Ofstream logFile;

SeqContainer <int> c;

For (SeqContainer <int >:: iteratori = c. begin (); I! = C. end ();)

{

If (badvalue (* I ))

{

LogFile <"Erasing" <* I <'\ n ';

I = c. erase (I); // assign the return value of erase to I to keep the I value valid.

}

Else

{

++ I;

}

}

2. If the container is a standard associated container, write a loop to traverse the elements in the container. Remember to add an increasing suffix to the iterator when the iterator is passed to erase. For example:

Ofstream logFile;

AssocContainer <int> c;

...

For (AssocContainer <int>: iterator I = c. begin (); I! = C. end ();)

{

If (badvalue (* I ))

{

LogFile <"Erasing" <* I <'\ n ';

C. erase (I ++ );

}

Else

++ I;

}


How to delete elements in the c ++ stl heap operation

Only the heap can be rebuilt. This is the feature of the heap. You can only operate on the top of the heap.
If you really need to delete elements from the middle, you should not choose to use the heap.

Methods for removing elements from STL in C ++

Container c
When c is a vector, string, or deque, use
C. erase (remove (c. begin (), c. end (), 1963), c. end (); // remove an element whose value is 1963 in c.

When c is a list, use
C. remove (1963 );

When c is associated with a container, use
C. erase (1963 );

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.