◐ Shell
clean mode source ↗

std::erase, std::erase_if(std::vector) - cppreference.com

From cppreference.com

Defined in header <vector>

template< class T, class Alloc, class U >
constexpr std::vector<T, Alloc>::size_type
    erase( std::vector<T, Alloc>& c, const U& value );
(1) (since C++20)
(until C++26)
template< class T, class Alloc, class U = T >
constexpr std::vector<T, Alloc>::size_type
    erase( std::vector<T, Alloc>& c, const U& value );
(since C++26)
template< class T, class Alloc, class Pred >
constexpr std::vector<T, Alloc>::size_type
    erase_if( std::vector<T, Alloc>& c, Pred pred );
(2) (since C++20)

1) Erases all elements that compare equal to value from the container. Equivalent to

auto it = std::remove(c.begin(), c.end(), value);
auto r = c.end() - it;
c.erase(it, c.end());
return r;

2) Erases all elements that satisfy the predicate pred from the container. Equivalent to

auto it = std::remove_if(c.begin(), c.end(), pred);
auto r = c.end() - it;
c.erase(it, c.end());
return r;

Parameters

c - container from which to erase
value - value to be removed
pred - unary predicate which returns ​true if the element should be erased.

The expression pred(v) must be convertible to bool for every argument v of type (possibly const) T, regardless of value category, and must not modify v. Thus, a parameter type of T&is not allowed, nor is T unless for T a move is equivalent to a copy(since C++11). ​

Return value

The number of erased elements.

Complexity

Linear.

Notes

Feature-test macro Value Std Feature
__cpp_lib_algorithm_default_value_type 202403 (C++26) List-initialization for algorithm (1)

Example

#include <complex>
#include <format>
#include <numeric>
#include <print>
#include <vector>

template<typename T>
struct std::formatter<std::complex<T>, char>
{
    template<class ParseContext>
    constexpr ParseContext::iterator parse(ParseContext& ctx)
    {
        return ctx.begin();
    }

    template<class FmtContext>
    FmtContext::iterator format(const auto& s, FmtContext& ctx) const
    {
        auto out{std::format("({},{})", s.real(), s.imag())};
        return std::ranges::copy(std::move(out), ctx.out()).out;
    }
};

int main()
{
    std::vector<char> cnt(10);
    std::iota(cnt.begin(), cnt.end(), '0');
    std::println("Initially, cnt = {}", cnt);
    
    std::erase(cnt, '3');
    std::println("After erase '3', cnt = {}", cnt);
    
    auto erased = std::erase_if(cnt, [](char x) { return (x - '0') % 2 == 0; });
    std::println("After erase all even numbers, cnt = {}", cnt);
    std::println("Erased even numbers: {}", erased);
    
    std::vector<std::complex<double>> nums{{2, 2}, {4, 2}, {4, 8}, {4, 2}};
    #ifdef __cpp_lib_algorithm_default_value_type
        std::erase(nums, {4, 2});
    #else
        std::erase(nums, std::complex<double>{4, 2});
    #endif
    std::println("After erase {{4, 2}}, nums = {}", nums);
}

Output:

Initially, cnt = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
After erase '3', cnt = [0, 1, 2, 4, 5, 6, 7, 8, 9]
After erase all even numbers, cnt = [1, 5, 7, 9]
Erased even numbers: 5
After erase {4, 2}, nums = [(2,2), (4,8)]

See also