Namespaces
Variants

std::ranges:: move, std::ranges:: move_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 O >

requires std:: indirectly_movable < I, O >
constexpr move_result < I, O >

move ( I first, S last, O result ) ;
(1) (desde C++20)
template < ranges:: input_range R, std:: weakly_incrementable O >

requires std:: indirectly_movable < ranges:: iterator_t < R > , O >
constexpr move_result < ranges:: borrowed_iterator_t < R > , O >

move ( R && r, O result ) ;
(2) (desde C++20)
Tipos auxiliares
template < class I, class O >
using move_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 que comienza en result . El comportamiento es indefinido si result está dentro del rango [ first , last ) . En tal caso, puede usarse ranges::move_backward 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.

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
result - el inicio del rango de destino

Valor de retorno

{ last, result + N } , donde

1) N = ranges:: distance ( first, last ) .
2) N = ranges:: distance ( r ) .

Complejidad

Exactamente N 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_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O>
    requires std::indirectly_movable<I, O>
    constexpr ranges::move_result<I, O>
        operator()(I first, S last, O result) const
    {
        for (; first != last; ++first, ++result)
            *result = ranges::iter_move(first);
        return {std::move(first), std::move(result)};
    }
    template<ranges::input_range R, std::weakly_incrementable O>
    requires std::indirectly_movable<ranges::iterator_t<R>, O>
    constexpr ranges::move_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, O result) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(result));
    }
};
inline constexpr move_fn move {};

Ejemplo

El siguiente código mueve objetos thread (que en sí mismos son no copiables ) de un contenedor a otro.

#include <algorithm>
#include <chrono>
#include <iostream>
#include <iterator>
#include <list>
#include <thread>
#include <vector>
using namespace std::literals::chrono_literals;
void f(std::chrono::milliseconds n)
{
    std::this_thread::sleep_for(n);
    std::cout << "thread with n=" << n.count() << "ms ended" << std::endl;
}
int main()
{
    std::vector<std::jthread> v;
    v.emplace_back(f, 400ms);
    v.emplace_back(f, 600ms);
    v.emplace_back(f, 800ms);
    std::list<std::jthread> l;
    // std::ranges::copy() would not compile, because std::jthread is non-copyable
    std::ranges::move(v, std::back_inserter(l));
}

Salida:

thread with n=400ms ended
thread with n=600ms ended
thread with n=800ms ended

Véase también

mueve un rango de elementos a una nueva ubicación en orden inverso
(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)