Namespaces
Variants
Views
Actions

Difference between revisions of "Template:cpp/container/erase2"

From cppreference.com
m (+inplace_vector(), with hacks and NOTES for committee folks.)
m (Example: rm hack-implementation of erase/erase_if for inplace_vector.)
Line 136: Line 136:
 
{{example
 
{{example
 
|code=
 
|code=
#include <fmt/ranges.h>
 
#include <fmt/std.h>
 
namespace std { using namespace fmt; } // HACK
 
 
#include "inplace_vector.hpp"
 
 
#include <algorithm>
 
#include <iterator>
 
namespace std { // HACK
 
// erasure for inplace_vector
 
template<class T, std::size_t N, class U>
 
constexpr inplace_vector<T, N>::size_type
 
erase(inplace_vector<T, N>& c, const U& value)
 
{
 
    auto it = std::remove(c.begin(), c.end(), value);
 
    auto r = std::distance(it, c.end());
 
    c.erase(it, c.end());
 
    return r;
 
}
 
 
template<class T, std::size_t N, class Predicate>
 
constexpr inplace_vector<T, N>::size_type
 
erase_if(inplace_vector<T, N>& c, Predicate pred)
 
{
 
    auto it = std::remove_if(c.begin(), c.end(), pred);
 
    auto r = std::distance(it, c.end());
 
    c.erase(it, c.end());
 
    return r;
 
}
 
}
 
 
 
#include <cassert>
 
#include <cassert>
 
#include <inplace_vector>
 
#include <inplace_vector>

Revision as of 13:43, 28 August 2024

 
 
 
 
Defined in header <inplace_vector>
template< class T, std::size_t N, class U /* = T <- should be!, see e.g. P2248 */ >

constexpr typename std::inplace_vector<T, N>::size_type /* <- down with typename! */

    erase( std::inplace_vector<T, N>& c, const U& value );
(1) (since C++26)
template< class T, std::size_t N, class Pred >

constexpr typename std::inplace_vector<T, N>::size_type /* <- down with typename! */

    erase_if( std::inplace_vector<T, N>& c, Pred pred );
(2) (since C++26)
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 = std::distance(it, c.end());
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 = std::distance(it, c.end());
c.erase(it, c.end());
return r;

Contents

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.

Example

#include <cassert>
#include <inplace_vector>
#include <numeric>
 
int main()
{
    std::inplace_vector<int, 10> v(10, 0);
    std::ranges::iota(v, 0);
    std::println("Initially, v = {}", v);
 
    auto erased = std::erase(v, 3);
    std::println("After erase(v, 3), v = {}", v);
    assert(erased == 1);
 
    erased = std::erase_if(v, [](int x) { return x % 2 == 0; });
    std::println("After erasing all even numbers, v = {}", v);
    std::println("Erased even numbers: {}", erased);
}

Output:

Initially, v = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
After erase(v, 3), v = [0, 1, 2, 4, 5, 6, 7, 8, 9]
After erasing all even numbers, v = [1, 5, 7, 9]
Erased even numbers: 5

See also

removes elements satisfying specific criteria
(function template) [edit]
removes elements satisfying specific criteria
(niebloid)[edit]