Namespaces
Variants

std::ranges:: remove_copy, std::ranges:: remove_copy_if, std::ranges:: remove_copy_result, std::ranges:: remove_copy_if_result

From cppreference.net
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
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
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
Operations on uninitialized storage
Return types
Definiert im Header <algorithm>
Aufrufsignatur
(1)
template < std:: input_iterator I, std:: sentinel_for < I > S,

std:: weakly_incrementable O, class T, class Proj = std:: identity >
requires std:: indirectly_copyable < I, O > &&
std:: indirect_binary_predicate
< ranges:: equal_to , std :: projected < I, Proj > , const T * >
constexpr remove_copy_result < I, O >

remove_copy ( I first, S last, O result, const T & value, Proj proj = { } ) ;
(seit C++20)
(bis C++26)
template < std:: input_iterator I, std:: sentinel_for < I > S,

std:: weakly_incrementable O, class Proj = std:: identity ,
class T = std :: projected_value_t < I, Proj > >
requires std:: indirectly_copyable < I, O > &&
std:: indirect_binary_predicate
< ranges:: equal_to , std :: projected < I, Proj > , const T * >
constexpr remove_copy_result < I, O >

remove_copy ( I first, S last, O result, const T & value, Proj proj = { } ) ;
(seit C++26)
(2)
template < ranges:: input_range R,

std:: weakly_incrementable O, class T, class Proj = std:: identity >
requires std:: indirectly_copyable < ranges:: iterator_t < R > , O > &&
std:: indirect_binary_predicate
< ranges:: equal_to ,
std :: projected < ranges:: iterator_t < R > , Proj > , const T * >
constexpr remove_copy_result < ranges:: borrowed_iterator_t < R > , O >

remove_copy ( R && r, O result, const T & value, Proj proj = { } ) ;
(seit C++20)
(bis C++26)
template < ranges:: input_range R,

std:: weakly_incrementable O, class Proj = std:: identity ,
class T = std :: projected_value_t < ranges:: iterator_t < R > , Proj > >
requires std:: indirectly_copyable < ranges:: iterator_t < R > , O > &&
std:: indirect_binary_predicate
< ranges:: equal_to ,
std :: projected < ranges:: iterator_t < R > , Proj > , const T * >
constexpr remove_copy_result < ranges:: borrowed_iterator_t < R > , O >

remove_copy ( R && r, O result, const T & value, Proj proj = { } ) ;
(seit C++26)
template < std:: input_iterator I, std:: sentinel_for < I > S,

std:: weakly_incrementable O, class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >
requires std:: indirectly_copyable < I, O >
constexpr remove_copy_if_result < I, O >

remove_copy_if ( I first, S last, O result, Pred pred, Proj proj = { } ) ;
(3) (seit C++20)
template < ranges:: input_range R,

std:: weakly_incrementable O, class Proj = std:: identity ,
std:: indirect_unary_predicate <
std :: projected < ranges:: iterator_t < R > , Proj >> Pred >
requires std:: indirectly_copyable < ranges:: iterator_t < R > , O >
constexpr remove_copy_if_result < ranges:: borrowed_iterator_t < R > , O >

remove_copy_if ( R && r, O result, Pred pred, Proj proj = { } ) ;
(4) (seit C++20)
Hilfstypen
template < class I, class O >
using remove_copy_result = ranges:: in_out_result < I, O > ;
(5) (seit C++20)
template < class I, class O >
using remove_copy_if_result = ranges:: in_out_result < I, O > ;
(6) (seit C++20)

Kopiert Elemente aus dem Quellbereich [ first , last ) in den Zielbereich beginnend bei result , unter Auslassung der Elemente, die (nach Projektion durch proj ) bestimmte Kriterien erfüllen. Das Verhalten ist undefiniert, wenn sich Quell- und Zielbereich überlappen.

1) Ignoriert alle Elemente, die gleich value sind.
3) Ignoriert alle Elemente, für die das Prädikat pred den Wert true zurückgibt.
2,4) Gleich wie (1,3) , verwendet jedoch r als Quellbereich, als ob ranges:: begin ( r ) als first und ranges:: end ( r ) als last verwendet würde.

Die auf dieser Seite beschriebenen funktionsähnlichen Entitäten sind Algorithm Function Objects (informell bekannt als Niebloids ), das heißt:

Inhaltsverzeichnis

Parameter

first, last - das Iterator-Sentinel-Paar, das den Quell- Bereich der zu verarbeitenden Elemente definiert
r - der Quellbereich der Elemente
result - der Anfang des Zielbereichs
value - der Wert der Elemente, die nicht kopiert werden sollen
comp - das binäre Prädikat zum Vergleichen der projizierten Elemente
proj - die Projektion, die auf die Elemente angewendet wird

Rückgabewert

{ last, result + N } , wobei N die Anzahl der kopierten Elemente ist.

Komplexität

Genau ranges:: distance ( first, last ) Anwendungen des entsprechenden Prädikats comp und jeglicher Projektion proj .

Hinweise

Der Algorithmus ist stabil, d.h. er bewahrt die relative Reihenfolge der kopierten Elemente.

Feature-Test Makro Wert Std Feature
__cpp_lib_algorithm_default_value_type 202403 (C++26) Listeninitialisierung für Algorithmen ( 1,2 )

Mögliche Implementierung

remove_copy (1,2)
struct remove_copy_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S,
             std::weakly_incrementable O, class Proj = std::identity,
             class T = std::projected_value_t<I, Proj>>
    requires std::indirectly_copyable<I, O> &&
             std::indirect_binary_predicate<ranges::equal_to,
                                            std::projected<I, Proj>, const T*>
    constexpr ranges::remove_copy_result<I, O>
        operator()(I first, S last, O result, const T& value, Proj proj = {}) const
    {
        for (; !(first == last); ++first)
            if (value != std::invoke(proj, *first))
            {
                *result = *first;
                ++result;
            }
        return {std::move(first), std::move(result)};
    }
    template<ranges::input_range R, 
             std::weakly_incrementable O, class Proj = std::identity,
             class T = std::projected_value_t<ranges::iterator_t<R>, Proj>>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O> &&
             std::indirect_binary_predicate<ranges::equal_to,
             std::projected<ranges::iterator_t<R>, Proj>, const T*>
    constexpr ranges::remove_copy_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, O result, const T& value, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(result), value,
                       std::move(proj));
    }
};
inline constexpr remove_copy_fn remove_copy {};
remove_copy_if (3,4)
struct remove_copy_if_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O,
             class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    requires std::indirectly_copyable<I, O>
    constexpr ranges::remove_copy_if_result<I, O>
        operator()(I first, S last, O result, Pred pred, Proj proj = {}) const
    {
        for (; first != last; ++first)
            if (false == std::invoke(pred, std::invoke(proj, *first)))
            {
                *result = *first;
                ++result;
            }
        return {std::move(first), std::move(result)};
    }
    template<ranges::input_range R, std::weakly_incrementable O,
             class Proj = std::identity,
             std::indirect_unary_predicate<
                 std::projected<ranges::iterator_t<R>, Proj>> Pred>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O>
    constexpr ranges::remove_copy_if_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, O result, Pred pred, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(result),
                       std::move(pred), std::move(proj));
    }
};
inline constexpr remove_copy_if_fn remove_copy_if {};

Beispiel

#include <algorithm>
#include <array>
#include <complex>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string_view>
#include <vector>
void println(const auto rem, const auto& v)
{
    std::cout << rem << ' ';
    for (const auto& e : v)
        std::cout << e << ' ';
    std::cout << '\n';
}
int main()
{
    // Filtere das Raute-Symbol aus dem gegebenen String heraus.
    const std::string_view str{"#Small #Buffer #Optimization"};
    std::cout << "vorher: " << std::quoted(str) << '\n';
    std::cout << "nachher:  \"";
    std::ranges::remove_copy(str.begin(), str.end(),
                             std::ostream_iterator<char>(std::cout), '#');
    std::cout << "\"\n";
    // Kopiere nur die komplexen Zahlen mit positivem Imaginärteil.
    using Ci = std::complex<int>;
    constexpr std::array<Ci, 5> source
    {
        Ci{1, 0}, Ci{0, 1}, Ci{2, -1}, Ci{3, 2}, Ci{4, -3}
    };
    std::vector<std::complex<int>> target;
    std::ranges::remove_copy_if
    (
        source,
        std::back_inserter(target),
        [](int imag) { return imag <= 0; },
        [](Ci z) { return z.imag(); }
    );
    println("Quelle:", source);
    println("Ziel:", target);
    std::vector<std::complex<float>> nums{{2, 2}, {1, 3}, {4, 8}, {1, 3}};
    std::vector<std::complex<double>> outs;
    #ifdef __cpp_lib_algorithm_default_value_type
        std::remove_copy(nums.cbegin(), nums.cend(), std::back_inserter(outs),
                         {1, 3}); // T wird zu std::complex<float> abgeleitet
    #else
        std::remove_copy(nums.cbegin(), nums.cend(), std::back_inserter(outs),
                         std::complex<float>{1, 3});
    #endif
    println("nums:  ", nums);
    println("outs:  ", outs);
}

Ausgabe:

vorher: "#Small #Buffer #Optimization"
nachher:  "Small Buffer Optimization"
Quelle: (1,0) (0,1) (2,-1) (3,2) (4,-3)
Ziel: (0,1) (3,2)
nums:   (2,2) (1,3) (4,8) (1,3)
outs:   (2,2) (4,8)

Siehe auch

Entfernt Elemente, die bestimmte Kriterien erfüllen
(Algorithmus-Funktionsobjekt)
Kopiert einen Bereich von Elementen an einen neuen Speicherort
(Algorithmus-Funktionsobjekt)
Kopiert eine Anzahl von Elementen an einen neuen Speicherort
(Algorithmus-Funktionsobjekt)
Kopiert einen Bereich von Elementen in umgekehrter Reihenfolge
(Algorithmus-Funktionsobjekt)
Kopiert einen Bereich und ersetzt Elemente, die bestimmte Kriterien erfüllen, durch einen anderen Wert
(Algorithmus-Funktionsobjekt)
Erstellt eine Kopie eines Bereichs, der umgekehrt ist
(Algorithmus-Funktionsobjekt)
Kopiert und rotiert einen Bereich von Elementen
(Algorithmus-Funktionsobjekt)
Erstellt eine Kopie eines Bereichs von Elementen, die keine aufeinanderfolgenden Duplikate enthält
(Algorithmus-Funktionsobjekt)
Kopiert einen Bereich von Elementen und lässt diejenigen aus, die bestimmte Kriterien erfüllen
(Funktions-Template)