Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/ranges/remove"

From cppreference.com
< cpp‎ | algorithm‎ | ranges
m (added explanation how to use return for erasure)
m (FTM.)
 
(2 intermediate revisions by 2 users not shown)
Line 4: Line 4:
 
{{dcl header|algorithm}}
 
{{dcl header|algorithm}}
 
{{dcl h|Call signature}}
 
{{dcl h|Call signature}}
{{dcla|since=c++20|num=1|1=
+
{{dcl rev begin|num=1}}
template< std::permutable I, std::sentinel_for<I> S, class T,
+
{{dcla|anchor=1|since=c++20|until=c++26|1=
          class Proj = std::identity >
+
template< std::permutable I, std::sentinel_for<I> S,
requires std::indirect_binary_predicate<ranges::equal_to,
+
          class T, class Proj = std::identity >
            std::projected<I, Proj>, const T*>
+
requires std::indirect_binary_predicate
 +
            <ranges::equal_to, std::projected<I, Proj>, const T*>
 
constexpr ranges::subrange<I>
 
constexpr ranges::subrange<I>
 
     remove( I first, S last, const T& value, Proj proj = {} );
 
     remove( I first, S last, const T& value, Proj proj = {} );
 
}}
 
}}
{{dcl|since=c++20|num=2|1=
+
{{dcl|since=c++26|1=
template< ranges::forward_range R, class T, class Proj = std::identity >
+
template< std::permutable I, std::sentinel_for<I> S,
 +
          class Proj = std::identity,
 +
          class T = std::projected_value_t<I, Proj> >
 +
requires std::indirect_binary_predicate
 +
            <ranges::equal_to, std::projected<I, Proj>, const T*>
 +
constexpr ranges::subrange<I>
 +
    remove( I first, S last, const T& value, Proj proj = {} );
 +
}}
 +
{{dcl rev end}}
 +
{{dcl rev begin|num=2}}
 +
{{dcl|since=c++20|until=c++26|1=
 +
template< ranges::forward_range R,
 +
          class T, class Proj = std::identity >
 +
requires std::permutable<ranges::iterator_t<R>> &&
 +
        std::indirect_binary_predicate
 +
            <ranges::equal_to,
 +
              std::projected<ranges::iterator_t<R>, Proj>, const T*>
 +
constexpr ranges::borrowed_subrange_t<R>
 +
    remove( R&& r, const T& value, Proj proj = {} );
 +
}}
 +
{{dcl|since=c++26|1=
 +
template< ranges::forward_range R,
 +
          class Proj = std::identity,
 +
          class T = std::projected_value_t<ranges::iterator_t<R>, Proj> >
 
requires std::permutable<ranges::iterator_t<R>> &&
 
requires std::permutable<ranges::iterator_t<R>> &&
         std::indirect_binary_predicate<ranges::equal_to,
+
         std::indirect_binary_predicate
            std::projected<ranges::iterator_t<R>, Proj>, const T*>
+
            <ranges::equal_to,
 +
              std::projected<ranges::iterator_t<R>, Proj>, const T*>
 
constexpr ranges::borrowed_subrange_t<R>
 
constexpr ranges::borrowed_subrange_t<R>
 
     remove( R&& r, const T& value, Proj proj = {} );
 
     remove( R&& r, const T& value, Proj proj = {} );
 
}}
 
}}
{{dcla|since=c++20|num=3|1=
+
{{dcl rev end}}
template< std::permutable I, std::sentinel_for<I> S, class Proj = std::identity,
+
{{dcla|num=3|since=c++20|1=
 +
template< std::permutable I, std::sentinel_for<I> S,
 +
          class Proj = std::identity,
 
           std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
 
           std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
 
constexpr ranges::subrange<I>
 
constexpr ranges::subrange<I>
 
     remove_if( I first, S last, Pred pred, Proj proj = {} );
 
     remove_if( I first, S last, Pred pred, Proj proj = {} );
 
}}
 
}}
{{dcl|since=c++20|num=4|1=
+
{{dcl|num=4|since=c++20|1=
template< ranges::forward_range R, class Proj = std::identity,
+
template< ranges::forward_range R,
           std::indirect_unary_predicate<
+
          class Proj = std::identity,
               std::projected<ranges::iterator_t<R>, Proj>> Pred >
+
           std::indirect_unary_predicate
 +
               <std::projected<ranges::iterator_t<R>, Proj>> Pred >
 
requires std::permutable<ranges::iterator_t<R>>
 
requires std::permutable<ranges::iterator_t<R>>
 
constexpr ranges::borrowed_subrange_t<R>
 
constexpr ranges::borrowed_subrange_t<R>
Line 61: Line 89:
 
Exactly {{c|N}} applications of the corresponding predicate and any projection, where {{c|1=N = ranges::distance(first, last)}}, and {{c|N - 1}} move operations at worst.
 
Exactly {{c|N}} applications of the corresponding predicate and any projection, where {{c|1=N = ranges::distance(first, last)}}, and {{c|N - 1}} move operations at worst.
  
<!--===Exceptions===-->
 
 
===Notes===
 
===Notes===
 
A call to {{tt|ranges::remove}} is typically followed by a call to a container's {{tt|erase}} member function, which erases the unspecified values and reduces the ''physical'' size of the container to match its new ''logical'' size. These two invocations together constitute a so-called {{enwiki|Erase-remove idiom}}, which can be achieved by the free function {{lc|std::erase}} that has [[cpp/container#Non-member function table|overloads]] for all standard ''sequence'' containers, or {{lc|std::erase_if}} that has [[cpp/container#Non-member function table|overloads]] for ''all'' standard containers.
 
A call to {{tt|ranges::remove}} is typically followed by a call to a container's {{tt|erase}} member function, which erases the unspecified values and reduces the ''physical'' size of the container to match its new ''logical'' size. These two invocations together constitute a so-called {{enwiki|Erase-remove idiom}}, which can be achieved by the free function {{lc|std::erase}} that has [[cpp/container#Non-member function table|overloads]] for all standard ''sequence'' containers, or {{lc|std::erase_if}} that has [[cpp/container#Non-member function table|overloads]] for ''all'' standard containers.
Line 72: Line 99:
  
 
===Possible implementation===
 
===Possible implementation===
{{eq impl
+
{{eq impl|title1=remove|ver1=1|1=
|title1=remove|ver1=1|1=
+
 
struct remove_fn
 
struct remove_fn
 
{
 
{
     template<std::permutable I, std::sentinel_for<I> S, class T,
+
     template<std::permutable I, std::sentinel_for<I> S, class Proj = std::identity,
             class Proj = std::identity>
+
             class T = std::projected_value_t<I, Proj>>
     requires std::indirect_binary_predicate<
+
     requires std::indirect_binary_predicate
                 ranges::equal_to, std::projected<I, Proj>, const T*>
+
                 <ranges::equal_to, std::projected<I, Proj>, const T*>
 
     constexpr ranges::subrange<I>
 
     constexpr ranges::subrange<I>
 
         operator()(I first, S last, const T& value, Proj proj = {}) const
 
         operator()(I first, S last, const T& value, Proj proj = {}) const
Line 95: Line 121:
 
         return {first, last};
 
         return {first, last};
 
     }
 
     }
 
+
   
     template<ranges::forward_range R, class T, class Proj = std::identity>
+
     template<ranges::forward_range R, class Proj = std::identity,
 +
            class T = std::projected_value_t<ranges::iterator_t<R>, Proj>>
 
     requires std::permutable<ranges::iterator_t<R>> &&
 
     requires std::permutable<ranges::iterator_t<R>> &&
             std::indirect_binary_predicate<
+
             std::indirect_binary_predicate
                 ranges::equal_to, std::projected<
+
                 <ranges::equal_to,
                    ranges::iterator_t<R>, Proj>, const T*>
+
                  std::projected<ranges::iterator_t<R>, Proj>, const T*>
 
     constexpr ranges::borrowed_subrange_t<R>
 
     constexpr ranges::borrowed_subrange_t<R>
 
         operator()(R&& r, const T& value, Proj proj = {}) const
 
         operator()(R&& r, const T& value, Proj proj = {}) const
Line 129: Line 156:
 
         return {first, last};
 
         return {first, last};
 
     }
 
     }
 
+
   
 
     template<ranges::forward_range R, class Proj = std::identity,
 
     template<ranges::forward_range R, class Proj = std::identity,
             std::indirect_unary_predicate<
+
             std::indirect_unary_predicate
                 std::projected<ranges::iterator_t<R>, Proj>> Pred>
+
                 <std::projected<ranges::iterator_t<R>, Proj>> Pred>
 
     requires std::permutable<ranges::iterator_t<R>>
 
     requires std::permutable<ranges::iterator_t<R>>
 
     constexpr ranges::borrowed_subrange_t<R>
 
     constexpr ranges::borrowed_subrange_t<R>
Line 143: Line 170:
 
inline constexpr remove_if_fn remove_if {};
 
inline constexpr remove_if_fn remove_if {};
 
}}
 
}}
 +
 +
===Notes===
 +
{{feature test macro|__cpp_lib_algorithm_default_value_type|value=202403|std=C++26|[[cpp/language/list initialization|List-initialization]] for algorithms {{vl|1,2}}}}
  
 
===Example===
 
===Example===
Line 148: Line 178:
 
|code=
 
|code=
 
#include <algorithm>
 
#include <algorithm>
 +
#include <cassert>
 +
#include <complex>
 
#include <cctype>
 
#include <cctype>
 
#include <iomanip>
 
#include <iomanip>
Line 153: Line 185:
 
#include <string>
 
#include <string>
 
#include <string_view>
 
#include <string_view>
 +
#include <vector>
  
 
int main()
 
int main()
Line 163: Line 196:
 
     v1.erase(ret.begin(), ret.end());
 
     v1.erase(ret.begin(), ret.end());
 
     std::cout << std::quoted(v1) << " (v1 after `erase`, size: " << v1.size() << ")\n\n";
 
     std::cout << std::quoted(v1) << " (v1 after `erase`, size: " << v1.size() << ")\n\n";
 
+
   
 
     // remove_if with custom unary predicate:
 
     // remove_if with custom unary predicate:
 
     auto rm = [](char c) { return !std::isupper(c); };
 
     auto rm = [](char c) { return !std::isupper(c); };
Line 173: Line 206:
 
     v2.erase(first, last);
 
     v2.erase(first, last);
 
     std::cout << std::quoted(v2) << " (v2 after `erase`, size: " << v2.size() << ")\n\n";
 
     std::cout << std::quoted(v2) << " (v2 after `erase`, size: " << v2.size() << ")\n\n";
 
+
   
 
     // creating a view into a container that is modified by `remove_if`:
 
     // creating a view into a container that is modified by `remove_if`:
 
     for (std::string s : {"Small Object Optimization", "Non-Type Template Parameter"})
 
     for (std::string s : {"Small Object Optimization", "Non-Type Template Parameter"})
 
         std::cout << std::quoted(s) << " => "
 
         std::cout << std::quoted(s) << " => "
 
             << std::string_view{begin(s), std::ranges::remove_if(s, rm).begin()} << '\n';
 
             << std::string_view{begin(s), std::ranges::remove_if(s, rm).begin()} << '\n';
 +
 +
    std::vector<std::complex<double>> nums{<!---->{2, 2}, {1, 3}, {4, 8}<!---->};
 +
    #ifdef __cpp_lib_algorithm_default_value_type
 +
        auto e = std::ranges::remove(nums, {1, 3}); // T gets deduced
 +
    #else
 +
        auto e = std::ranges::remove(nums, std::complex<double>{1, 3});
 +
    #endif
 +
    nums.erase(e.begin(), e.end());
 +
    assert((nums == std::vector<std::complex<double>>{<!---->{2, 2}, {4, 8}<!---->}));
 
}
 
}
 
|p=true
 
|p=true

Latest revision as of 21:29, 20 May 2024

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
       
       
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
       
       
Permutation operations
Fold operations
Numeric operations
(C++23)            
Operations on uninitialized storage
Return types
 
Defined in header <algorithm>
Call signature
(1)
template< std::permutable I, std::sentinel_for<I> S,

          class T, class Proj = std::identity >
requires std::indirect_binary_predicate
             <ranges::equal_to, std::projected<I, Proj>, const T*>
constexpr ranges::subrange<I>

    remove( I first, S last, const T& value, Proj proj = {} );
(since C++20)
(until C++26)
template< std::permutable I, std::sentinel_for<I> S,

          class Proj = std::identity,
          class T = std::projected_value_t<I, Proj> >
requires std::indirect_binary_predicate
             <ranges::equal_to, std::projected<I, Proj>, const T*>
constexpr ranges::subrange<I>

    remove( I first, S last, const T& value, Proj proj = {} );
(since C++26)
(2)
template< ranges::forward_range R,

          class T, class Proj = std::identity >
requires std::permutable<ranges::iterator_t<R>> &&
         std::indirect_binary_predicate
             <ranges::equal_to,
              std::projected<ranges::iterator_t<R>, Proj>, const T*>
constexpr ranges::borrowed_subrange_t<R>

    remove( R&& r, const T& value, Proj proj = {} );
(since C++20)
(until C++26)
template< ranges::forward_range R,

          class Proj = std::identity,
          class T = std::projected_value_t<ranges::iterator_t<R>, Proj> >
requires std::permutable<ranges::iterator_t<R>> &&
         std::indirect_binary_predicate
             <ranges::equal_to,
              std::projected<ranges::iterator_t<R>, Proj>, const T*>
constexpr ranges::borrowed_subrange_t<R>

    remove( R&& r, const T& value, Proj proj = {} );
(since C++26)
template< std::permutable I, std::sentinel_for<I> S,

          class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
constexpr ranges::subrange<I>

    remove_if( I first, S last, Pred pred, Proj proj = {} );
(3) (since C++20)
template< ranges::forward_range R,

          class Proj = std::identity,
          std::indirect_unary_predicate
              <std::projected<ranges::iterator_t<R>, Proj>> Pred >
requires std::permutable<ranges::iterator_t<R>>
constexpr ranges::borrowed_subrange_t<R>

    remove_if( R&& r, Pred pred, Proj proj = {} );
(4) (since C++20)

Removes all elements satisfying specific criteria from the range [firstlast) and returns a subrange [retlast), where ret is a past-the-end iterator for the new end of the range.

1) Removes all elements that are equal to value, using std::invoke(proj, *i) == value to compare.
3) Removes all elements for which std::invoke(pred, std::invoke(proj, *i)) returns true.
2,4) Same as (1,3), but uses r as the range, as if using ranges::begin(r) as first and ranges::end(r) as last.

Removing is done by shifting (by means of move assignment) the elements in the range in such a way that the elements that are not to be removed appear in the beginning of the range. Relative order of the elements that remain is preserved and the physical size of the container is unchanged. Iterators pointing to an element between the new logical end and the physical end of the range are still dereferenceable, but the elements themselves have unspecified values (as per MoveAssignable post-condition).

The function-like entities described on this page are niebloids, that is:

In practice, they may be implemented as function objects, or with special compiler extensions.

Contents

[edit] Parameters

first, last - the range of elements to process
r - the range of elements to process
value - the value of elements to remove
pred - predicate to apply to the projected elements
proj - projection to apply to the elements

[edit] Return value

{ret, last}, where [firstret) is the resulting subrange after removal, and the elements in subrange [retlast) are all in valid but unspecified state, i.e. [retlast) is the subrange to be erased.

[edit] Complexity

Exactly N applications of the corresponding predicate and any projection, where N = ranges::distance(first, last), and N - 1 move operations at worst.

[edit] Notes

A call to ranges::remove is typically followed by a call to a container's erase member function, which erases the unspecified values and reduces the physical size of the container to match its new logical size. These two invocations together constitute a so-called Erase-remove idiom, which can be achieved by the free function std::erase that has overloads for all standard sequence containers, or std::erase_if that has overloads for all standard containers.

The similarly-named container member functions list::remove, list::remove_if, forward_list::remove, and forward_list::remove_if erase the removed elements.

These algorithms usually cannot be used with associative containers such as std::set and std::map because their iterator types do not dereference to MoveAssignable types (the keys in these containers are not modifiable).

Because ranges::remove takes value by reference, it can have unexpected behavior if it is a reference to an element of the range [firstlast).

[edit] Possible implementation

remove
struct remove_fn
{
    template<std::permutable I, std::sentinel_for<I> S, class Proj = std::identity,
             class T = std::projected_value_t<I, Proj>>
    requires std::indirect_binary_predicate
                 <ranges::equal_to, std::projected<I, Proj>, const T*>
    constexpr ranges::subrange<I>
        operator()(I first, S last, const T& value, Proj proj = {}) const
    {
        first = ranges::find(std::move(first), last, value, proj);
        if (first != last)
        {
            for (I i{std::next(first)}; i != last; ++i)
                if (value != std::invoke(proj, *i))
                {
                    *first = ranges::iter_move(i);
                    ++first;
                }
        }
        return {first, last};
    }
 
    template<ranges::forward_range R, class Proj = std::identity,
             class T = std::projected_value_t<ranges::iterator_t<R>, Proj>>
    requires std::permutable<ranges::iterator_t<R>> &&
             std::indirect_binary_predicate
                 <ranges::equal_to,
                  std::projected<ranges::iterator_t<R>, Proj>, const T*>
    constexpr ranges::borrowed_subrange_t<R>
        operator()(R&& r, const T& value, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), value, std::move(proj));
    }
};
 
inline constexpr remove_fn remove {};
remove_if
struct remove_if_fn
{
    template<std::permutable I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    constexpr ranges::subrange<I>
        operator()(I first, S last, Pred pred, Proj proj = {}) const
    {
        first = ranges::find_if(std::move(first), last, pred, proj);
        if (first != last)
        {
            for (I i{std::next(first)}; i != last; ++i)
                if (!std::invoke(pred, std::invoke(proj, *i)))
                {
                    *first = ranges::iter_move(i);
                    ++first;
                }
        }
        return {first, last};
    }
 
    template<ranges::forward_range R, class Proj = std::identity,
             std::indirect_unary_predicate
                 <std::projected<ranges::iterator_t<R>, Proj>> Pred>
    requires std::permutable<ranges::iterator_t<R>>
    constexpr ranges::borrowed_subrange_t<R>
        operator()(R&& r, Pred pred, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), pred, std::move(proj));
    }
};
 
inline constexpr remove_if_fn remove_if {};

[edit] Notes

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

[edit] Example

#include <algorithm>
#include <cassert>
#include <complex>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
 
int main()
{
    std::string v1{"No - Diagnostic - Required"};
    std::cout << std::quoted(v1) << " (v1, size: " << v1.size() << ")\n";
    const auto ret = std::ranges::remove(v1, ' ');
    std::cout << std::quoted(v1) << " (v1 after `remove`, size: " << v1.size() << ")\n";
    std::cout << ' ' << std::string(std::distance(v1.begin(), ret.begin()), '^') << '\n';
    v1.erase(ret.begin(), ret.end());
    std::cout << std::quoted(v1) << " (v1 after `erase`, size: " << v1.size() << ")\n\n";
 
    // remove_if with custom unary predicate:
    auto rm = [](char c) { return !std::isupper(c); };
    std::string v2{"Substitution Failure Is Not An Error"};
    std::cout << std::quoted(v2) << " (v2, size: " << v2.size() << ")\n";
    const auto [first, last] = std::ranges::remove_if(v2, rm);
    std::cout << std::quoted(v2) << " (v2 after `remove_if`, size: " << v2.size() << ")\n";
    std::cout << ' ' << std::string(std::distance(v2.begin(), first), '^') << '\n';
    v2.erase(first, last);
    std::cout << std::quoted(v2) << " (v2 after `erase`, size: " << v2.size() << ")\n\n";
 
    // creating a view into a container that is modified by `remove_if`:
    for (std::string s : {"Small Object Optimization", "Non-Type Template Parameter"})
        std::cout << std::quoted(s) << " => "
            << std::string_view{begin(s), std::ranges::remove_if(s, rm).begin()} << '\n';
 
    std::vector<std::complex<double>> nums{{2, 2}, {1, 3}, {4, 8}};
    #ifdef __cpp_lib_algorithm_default_value_type
        auto e = std::ranges::remove(nums, {1, 3}); // T gets deduced
    #else
        auto e = std::ranges::remove(nums, std::complex<double>{1, 3});
    #endif
    nums.erase(e.begin(), e.end());
    assert((nums == std::vector<std::complex<double>>{{2, 2}, {4, 8}}));
}

Possible output:

"No _ Diagnostic _ Required" (v1, size: 26)
"No_Diagnostic_Requiredired" (v1 after `remove`, size: 26)
 ^^^^^^^^^^^^^^^^^^^^^^
"No_Diagnostic_Required" (v1 after `erase`, size: 22)
 
"Substitution Failure Is Not An Error" (v2, size: 36)
"SFINAEtution Failure Is Not An Error" (v2 after `remove_if`, size: 36)
 ^^^^^^
"SFINAE" (v2 after `erase`, size: 6)
 
"Small Object Optimization" => SOO
"Non-Type Template Parameter" => NTTP

[edit] See also

copies a range of elements omitting those that satisfy specific criteria
(niebloid)[edit]
removes consecutive duplicate elements in a range
(niebloid)[edit]
removes elements satisfying specific criteria
(function template) [edit]