SoFunction
Updated on 2025-04-14

C++20 Unified Container Erase: Implementation of std::erase  and std::eraseif

C++20 introduces two very practical function templates:std::eraseandstd::erase_if, They provide a simpler and unified interface for container operations, greatly simplifying the deletion of container elements.

1. Usage of std::erase

std::eraseUsed to delete all elements that match the specified value from the container. It works with all standard containers such asstd::vectorstd::liststd::mapwait.

1.1 Syntax

template<class Container, class T>
constexpr auto erase(Container& c, const T& value);

1.2 Parameters

  • c: The container to be operated.
  • value: The value of the element to be deleted.

1.3 Return value

Returns the number of elements deleted.

1.4 Example

#include &lt;iostream&gt;
#include &lt;vector&gt;

int main() {
    std::vector&lt;int&gt; vec = {1, 2, 3, 4, 5, 3, 6};
    auto erased_count = std::erase(vec, 3); // Delete all elements with a value of 3    std::cout &lt;&lt; "Erased " &lt;&lt; erased_count &lt;&lt; " elements.\n";
    for (int i : vec) {
        std::cout &lt;&lt; i &lt;&lt; " ";
    }
    return 0;
}

Output:

Erased 2 elements.
1 2 4 5 6

2. Usage of std::erase_if

std::erase_ifUsed to delete elements that meet specific conditions from the container.

2.1 Syntax

template<class Container, class Predicate>
constexpr auto erase_if(Container& c, Predicate pred);

2.2 Parameters

  • c: The container to be operated.
  • pred: Predicate function, returntrueIndicates that the element should be deleted.

2.3 Return value

Returns the number of elements deleted.

2.4 Example

#include &lt;iostream&gt;
#include &lt;vector&gt;

int main() {
    std::vector&lt;int&gt; vec = {1, 2, 3, 4, 5, 6};
    auto erased_count = std::erase_if(vec, [](int x) { return x % 2 == 0; }); // Delete all even numbers    std::cout &lt;&lt; "Erased " &lt;&lt; erased_count &lt;&lt; " elements.\n";
    for (int i : vec) {
        std::cout &lt;&lt; i &lt;&lt; " ";
    }
    return 0;
}

Output:

Erased 3 elements.
1 3 5

3. Advantages and application scenarios

3.1 Unified interface

std::eraseandstd::erase_ifProvides a unified interface, making the deletion operations of different containers more consistent. This reduces developers' dependence on different container member functions and reduces learning costs.

3.2 Simplify the code

usestd::eraseandstd::erase_ifIt can avoid manually using iterators for deletion operations, simplifying the code. For example,std::erase_ifCan be replacedstd::remove_ifanderaseThe combination reduces the amount of code.

3.3 Wide scope of application

These two functions are suitable for all standard containers, includingstd::vectorstd::liststd::mapwait. This makes them more general in handling different types of containers.

4. Summary

C++20'sstd::eraseandstd::erase_ifProvides a simpler and more unified interface for container operations. They not only simplify the code, but also reduce the developer's dependence on different container member functions. In actual development, these two functions can significantly improve the readability and maintainability of the code.

This is the article about the implementation of C++20 unified container erasing: std::erase and std::erase_if. This is all about this article. For more related contents of C++20 unified container erasing, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!