Namespaces
Variants
Views
Actions

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

From cppreference.com
m (Example: +<print> for inplace_vector.)
m (`= T` for inplace_vector: P2248R14, it also already present in N4988 (C++26 draft); +demo for `U = T`.)
 
(2 intermediate revisions by one user not shown)
Line 11: Line 11:
 
|inplace_vector=
 
|inplace_vector=
 
{{dcl|num=1|since=c++26|1=
 
{{dcl|num=1|since=c++26|1=
template< class T, std::size_t N, class U /* = T <- should be!, see e.g. P2248 */ >
+
template< class T, std::size_t N, class U = T >
constexpr typename std::inplace_vector<T, N>::size_type /* <- down with typename! */
+
constexpr typename std::inplace_vector<T, N>::size_type
 
     erase( std::inplace_vector<T, N>& c, const U& value );
 
     erase( std::inplace_vector<T, N>& c, const U& value );
 
}}
 
}}
 
{{dcl|num=2|since=c++26|
 
{{dcl|num=2|since=c++26|
 
template< class T, std::size_t N, class Pred >
 
template< class T, std::size_t N, class Pred >
constexpr typename std::inplace_vector<T, N>::size_type /* <- down with typename! */
+
constexpr typename std::inplace_vector<T, N>::size_type
 
     erase_if( std::inplace_vector<T, N>& c, Pred pred );
 
     erase_if( std::inplace_vector<T, N>& c, Pred pred );
 
}}
 
}}
Line 126: Line 126:
 
     std::cout << "After erase all 'a', 'r', and 't': " << std::quoted(word) << '\n';
 
     std::cout << "After erase all 'a', 'r', and 't': " << std::quoted(word) << '\n';
 
     std::cout << "Erased symbols count: " << erased << '\n';
 
     std::cout << "Erased symbols count: " << erased << '\n';
 +
 +
#if __cpp_lib_algorithm_default_value_type
 +
    std::erase(word, {'g'});
 +
    std::cout << "After erase {'g'}: " << std::quoted(word) << '\n';
 +
#endif
 
}
 
}
 +
|p=true
 
|output=
 
|output=
 
Initially, word = "startling"
 
Initially, word = "startling"
Line 132: Line 138:
 
After erase all 'a', 'r', and 't': "sing"
 
After erase all 'a', 'r', and 't': "sing"
 
Erased symbols count: 4
 
Erased symbols count: 4
 +
After erase {'g'}: "sin"
 
}}
 
}}
 
|inplace_vector=
 
|inplace_vector=
Line 137: Line 144:
 
|code=
 
|code=
 
#include <cassert>
 
#include <cassert>
 +
#include <complex>
 
#include <inplace_vector>
 
#include <inplace_vector>
 
#include <numeric>
 
#include <numeric>
Line 154: Line 162:
 
     std::println("After erasing all even numbers, v = {}", v);
 
     std::println("After erasing all even numbers, v = {}", v);
 
     std::println("Erased even numbers: {}", erased);
 
     std::println("Erased even numbers: {}", erased);
 +
 +
    std::inplace_vector<std::complex<double>> nums{<!---->{2, 2}, {4, 2}, {4, 8}, {4, 2}<!---->};
 +
    std::erase(nums, {4, 2}); // since U = T, the value type can be ommited
 +
    std::println("After erase {4, 2}, nums = {}", nums);
 
}
 
}
 
|output=
 
|output=
Line 160: Line 172:
 
After erasing all even numbers, v = [1, 5, 7, 9]
 
After erasing all even numbers, v = [1, 5, 7, 9]
 
Erased even numbers: 5
 
Erased even numbers: 5
 +
After erase {4, 2}, nums = [(2,2), (4,8)]
 
}}
 
}}
 
|
 
|

Latest revision as of 15:59, 22 September 2024

 
 
 
 
Defined in header <inplace_vector>
template< class T, std::size_t N, class U = T >

constexpr typename std::inplace_vector<T, N>::size_type

    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

    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

[edit] 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). ​

[edit] Return value

The number of erased elements.

[edit] Complexity

Linear.

[edit] Example

#include <cassert>
#include <complex>
#include <inplace_vector>
#include <numeric>
#include <print>
 
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);
 
    std::inplace_vector<std::complex<double>> nums{{2, 2}, {4, 2}, {4, 8}, {4, 2}};
    std::erase(nums, {4, 2}); // since U = T, the value type can be ommited
    std::println("After erase {4, 2}, nums = {}", nums);
}

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
After erase {4, 2}, nums = [(2,2), (4,8)]

[edit] See also

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