Namespaces
Variants

std::ranges:: move_backward, std::ranges:: move_backward_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:: bidirectional_iterator I1, std:: sentinel_for < I1 > S1,

std:: bidirectional_iterator I2 >
requires std:: indirectly_movable < I1, I2 >
constexpr move_backward_result < I1, I2 >

move_backward ( I1 first, S1 last, I2 d_last ) ;
(1) (desde C++20)
template < ranges:: bidirectional_range R, std:: bidirectional_iterator I >

requires std:: indirectly_movable < ranges:: iterator_t < R > , I >
constexpr move_backward_result < ranges:: borrowed_iterator_t < R > , I >

move_backward ( R && r, I d_last ) ;
(2) (desde C++20)
Tipos auxiliares
template < class I, class O >
using move_backward_result = ranges:: in_out_result < I, O > ;
(3) (desde C++20)
1) Mueve los elementos en el rango, definido por [ first , last ) , a otro rango [ d_last - N , d_last ) , donde N = ranges:: distance ( first, last ) . Los elementos se mueven en orden inverso (el último elemento se mueve primero), pero se preserva su orden relativo. El comportamiento es indefinido si d_last está dentro de ( first, last ] . En tal caso, se puede usar ranges::move en su lugar.
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 .

Los elementos en el rango moved-from seguirán conteniendo valores válidos del tipo apropiado, pero no necesariamente los mismos valores que antes del movimiento, como si se usara * ( d_last - n ) = ranges:: iter_move ( last - n ) para cada entero n , donde 0 ≤ n < N .

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 de elementos a mover
r - el rango de los elementos a mover
d_last - el final del rango de destino

Valor de retorno

{ last, d_last - N } .

Complejidad

1) Exactamente N asignaciones de movimiento.
2) Exactamente ranges:: distance ( r ) asignaciones de movimiento.

Notas

Al mover rangos superpuestos, ranges::move es apropiado al mover hacia la izquierda (el inicio del rango de destino está fuera del rango fuente) mientras que ranges::move_backward es apropiado al mover hacia la derecha (el final del rango de destino está fuera del rango fuente).

Implementación posible

struct move_backward_fn
{
    template<std::bidirectional_iterator I1, std::sentinel_for<I1> S1,
             std::bidirectional_iterator I2>
    requires std::indirectly_movable<I1, I2>
    constexpr ranges::move_backward_result<I1, I2>
        operator()(I1 first, S1 last, I2 d_last) const
    {
        auto i {last};
        for (; i != first; *--d_last = ranges::iter_move(--i))
        {}
        return {std::move(last), std::move(d_last)};
    }
    template<ranges::bidirectional_range R, std::bidirectional_iterator I>
    requires std::indirectly_movable<ranges::iterator_t<R>, I>
    constexpr ranges::move_backward_result<ranges::borrowed_iterator_t<R>, I>
        operator()(R&& r, I d_last) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(d_last));
    }
};
inline constexpr move_backward_fn move_backward {};

Ejemplo

#include <algorithm>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
using Vec = std::vector<std::string>;
void print(std::string_view rem, Vec const& vec)
{
    std::cout << rem << "[" << vec.size() << "]: ";
    for (const std::string& s : vec)
        std::cout << (s.size() ? s : std::string{"·"}) << ' ';
    std::cout << '\n';
}
int main()
{
    Vec a{"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"};
    Vec b(a.size());
    print("Before move:\n" "a", a);
    print("b", b);
    std::ranges::move_backward(a, b.end());
    print("\n" "Move a >> b:\n" "a", a);
    print("b", b);
    std::ranges::move_backward(b.begin(), b.end(), a.end());
    print("\n" "Move b >> a:\n" "a", a);
    print("b", b);
    std::ranges::move_backward(a.begin(), a.begin()+3, a.end());
    print("\n" "Overlapping move a[0, 3) >> a[5, 8):\n" "a", a);
}

Salida posible:

Before move:
a[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █
b[8]: · · · · · · · ·
Move a >> b:
a[8]: · · · · · · · ·
b[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █
Move b >> a:
a[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █
b[8]: · · · · · · · ·
Overlapping move a[0, 3) >> a[5, 8):
a[8]: · · · ▄ ▅ ▁ ▂ ▃

Véase también

mueve un rango de elementos a una nueva ubicación
(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 en orden inverso
(objeto función de algoritmo)
(C++11)
mueve un rango de elementos a una nueva ubicación
(plantilla de función)
(C++11)
convierte el argumento a un xvalue
(plantilla de función)