Difference between std::remove and erase for vector?

后端 未结 8 1901
失恋的感觉
失恋的感觉 2020-12-12 18:42

I have a doubt that I would like to clarify in my head. I am aware of the different behavior for std::vector between erase and std::remove

8条回答
  •  失恋的感觉
    2020-12-12 19:20

    std::remove simply returns a new end() iterator to point to one past the last non-removed element (the number of items from the returned value to end() will match the number of items to be removed, but there is no guarantee their values are the same as those you were removing - they are in a valid but unspecified state). This is done so that it can work for multiple container types (basically any container type that a ForwardIterator can iterate through).

    std::vector::erase actually sets the new end() iterator after adjusting the size. This is because the vector's method actually knows how to handle adjusting it's iterators (the same can be done with std::list::erase, std::deque::erase, etc.).

    remove organizes a given container to remove unwanted objects. The container's erase function actually handles the "removing" the way that container needs it to be done. That is why they are separate.

提交回复
热议问题