C++20 introduces two very practical function templates:std::erase
andstd::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::erase
Used to delete all elements that match the specified value from the container. It works with all standard containers such asstd::vector
、std::list
、std::map
wait.
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 <iostream> #include <vector> int main() { std::vector<int> 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 << "Erased " << erased_count << " elements.\n"; for (int i : vec) { std::cout << i << " "; } return 0; }
Output:
Erased 2 elements.
1 2 4 5 6
2. Usage of std::erase_if
std::erase_if
Used 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, returntrue
Indicates that the element should be deleted.
2.3 Return value
Returns the number of elements deleted.
2.4 Example
#include <iostream> #include <vector> int main() { std::vector<int> 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 << "Erased " << erased_count << " elements.\n"; for (int i : vec) { std::cout << i << " "; } return 0; }
Output:
Erased 3 elements.
1 3 5
3. Advantages and application scenarios
3.1 Unified interface
std::erase
andstd::erase_if
Provides 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::erase
andstd::erase_if
It can avoid manually using iterators for deletion operations, simplifying the code. For example,std::erase_if
Can be replacedstd::remove_if
anderase
The combination reduces the amount of code.
3.3 Wide scope of application
These two functions are suitable for all standard containers, includingstd::vector
、std::list
、std::map
wait. This makes them more general in handling different types of containers.
4. Summary
C++20'sstd::erase
andstd::erase_if
Provides 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!