Namespaces
Variants

std::ranges:: partition_copy, std::ranges:: partition_copy_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
Definido en el encabezado <algorithm>
Firma de llamada
template < std:: input_iterator I, std:: sentinel_for < I > S,

std:: weakly_incrementable O1, std:: weakly_incrementable O2,
class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >
requires std:: indirectly_copyable < I, O1 > &&
std:: indirectly_copyable < I, O2 >
constexpr partition_copy_result < I, O1, O2 >
partition_copy ( I first, S last, O1 out_true, O2 out_false,

Pred pred, Proj proj = { } ) ;
(1) (desde C++20)
template < ranges:: input_range R,

std:: weakly_incrementable O1, std:: weakly_incrementable O2,
class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < iterator_t < R > , Proj >> Pred >
requires std:: indirectly_copyable < ranges:: iterator_t < R > , O1 > &&
std:: indirectly_copyable < ranges:: iterator_t < R > , O2 >
constexpr partition_copy_result < ranges:: borrowed_iterator_t < R > , O1, O2 >
partition_copy ( R && r, O1 out_true, O2 out_false,

Pred pred, Proj proj = { } ) ;
(2) (desde C++20)
Tipos auxiliares
template < class I, class O1, class O2 >
using partition_copy_result = ranges:: in_out_out_result < I, O1, O2 > ;
(3) (desde C++20)
1) Copia los elementos del rango de entrada [ first , last ) a dos rangos de salida diferentes dependiendo del valor devuelto por el predicado pred . Los elementos que satisfacen el predicado pred después de la proyección por proj se copian al rango que comienza en out_true . El resto de los elementos se copian al rango que comienza en out_false . El comportamiento es indefinido si el rango de entrada se superpone con cualquiera de los rangos de salida.
2) Igual que (1) , pero utiliza r como el rango fuente, como si se usara ranges:: begin ( r ) como first , y ranges:: end ( r ) como last .

Las entidades similares a funciones descritas en esta página son algorithm function objects (conocidas informalmente como niebloids ), es decir:

Contenidos

Parámetros

first, last - el par iterador-centinela que define el rango fuente de elementos a copiar
r - el rango fuente de elementos a copiar
out_true - el inicio del rango de salida para los elementos que satisfacen pred
out_false - el inicio del rango de salida para los elementos que no satisfacen pred
pred - predicado a aplicar a los elementos proyectados
proj - proyección a aplicar a los elementos

Valor de retorno

{ last, o1, o2 } , donde o1 y o2 son los extremos de los rangos de salida respectivamente, después de que se complete la copia.

Complejidad

Exactamente ranges:: distance ( first, last ) aplicaciones del predicado correspondiente comp y cualquier proyección proj .

Implementación posible

struct partition_copy_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S,
             std::weakly_incrementable O1, std::weakly_incrementable O2,
             class Proj = std::identity, std::indirect_unary_predicate<
             std::projected<I, Proj>> Pred>
    requires std::indirectly_copyable<I, O1> && std::indirectly_copyable<I, O2>
    constexpr ranges::partition_copy_result<I, O1, O2>
        operator()(I first, S last, O1 out_true, O2 out_false,
                   Pred pred, Proj proj = {}) const
    {
        for (; first != last; ++first)
            if (!!std::invoke(pred, std::invoke(proj, *first)))
                *out_true = *first, ++out_true;
            else
                *out_false = *first, ++out_false;
        return {std::move(first), std::move(out_true), std::move(out_false)};
    }
    template<ranges::input_range R,
             std::weakly_incrementable O1, std::weakly_incrementable O2,
             class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<iterator_t<R>, Proj>> Pred>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O1> &&
             std::indirectly_copyable<ranges::iterator_t<R>, O2>
    constexpr ranges::partition_copy_result<ranges::borrowed_iterator_t<R>, O1, O2>
        operator()(R&& r, O1 out_true, O2 out_false, Pred pred, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(out_true),
                       std::move(out_false), std::move(pred), std::move(proj));
    }
};
inline constexpr partition_copy_fn partition_copy {};

Ejemplo

#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
    const auto in = {'N', '3', 'U', 'M', '1', 'B', '4', 'E', '1', '5', 'R', '9'};
    std::vector<int> o1(size(in)), o2(size(in));
    auto pred = [](char c) { return std::isalpha(c); };
    auto ret = std::ranges::partition_copy(in, o1.begin(), o2.begin(), pred);
    std::ostream_iterator<char> cout {std::cout, " "};
    std::cout << "in = ";
    std::ranges::copy(in, cout);
    std::cout << "\no1 = ";
    std::copy(o1.begin(), ret.out1, cout);
    std::cout << "\no2 = ";
    std::copy(o2.begin(), ret.out2, cout);
    std::cout << '\n';
}

Salida:

in = N 3 U M 1 B 4 E 1 5 R 9
o1 = N U M B E R
o2 = 3 1 4 1 5 9

Véase también

divide un rango de elementos en dos grupos
(objeto función de algoritmo)
divide elementos en dos grupos preservando su orden relativo
(objeto función de algoritmo)
copia un rango de elementos a una nueva ubicación
(objeto función de algoritmo)
copia un rango de elementos omitiendo aquellos que cumplen criterios específicos
(objeto función de algoritmo)
copia un rango dividiendo los elementos en dos grupos
(plantilla de función)