Stl:remove and Erase differences

Source: Internet
Author: User

The Remove and erase functions in C + + STL used to confuse me and delete, what's the difference between the two?

The action of remove in a vector is to place an element equal to value at the tail of the vector, but does not reduce the size of the vector

The role of erase in vectors is to remove elements from a position position or section (begin, end), reducing their size

The Remove member function in the list container, the prototype is void remove (const value_type& val), and the function is to delete the node in the list with the same value as Val, freeing the node's resources.

The erase member function in the list container, the prototype is iterator erase (iterator position), and the function is to delete the node of the position location.

Considering the list::erase is related to the position, so erase also exists api:iterator erase (iterator first, iterator last);

For set, there is only erase API, there is no remove API. The role of erase is to delete the elements that meet the requirements.

(1) Void erase (iterator position);
(2) Size_type Erase (const value_type& val);
(3) Void erase (iterator first, iterator last);

In summary, erase is generally to release resources, really delete elements,

Remove is used primarily in vectors to move non-conforming elements to the end of a container without deleting elements that do not meet the requirements

Stl:remove and Erase differences

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.