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
헤더에 정의됨 <algorithm>
호출 서명
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) (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) (C++20부터)
도우미 타입
template < class I, class O >
using move_result = ranges:: in_out_result < I, O > ;
(3) (C++20부터)
1) [ first , last ) 범위에 있는 요소들을 result 로 시작하는 다른 범위로 이동합니다. result [ first , last ) 범위 내에 있을 경우 동작은 정의되지 않습니다. 이러한 경우에는 대신 ranges::move_backward 를 사용할 수 있습니다.
2) (1) 과 동일하지만, r 을 소스 범위로 사용하며, 마치 ranges:: begin ( r ) first 로, ranges:: end ( r ) last 로 사용하는 것과 같습니다.

이동된 범위의 요소들은 여전히 적절한 타입의 유효한 값을 포함하지만, 이동 전과 반드시 동일한 값일 필요는 없습니다.

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:

목차

매개변수

first, last - 이동할 요소들의 범위 를 정의하는 반복자-감시자 쌍
r - 이동할 요소들의 범위
result - 대상 범위의 시작 지점

반환값

{ last, result + N } , 여기서

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

복잡도

정확히 N 번의 move assignment.

참고 사항

겹치는 범위를 이동할 때, ranges::move 는 왼쪽으로 이동할 때(대상 범위의 시작이 소스 범위 밖에 있을 때) 적합한 반면, ranges::move_backward 는 오른쪽으로 이동할 때(대상 범위의 끝이 소스 범위 밖에 있을 때) 적합합니다.

가능한 구현

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 {};

예제

다음 코드는 스레드 객체들(자체적으로 복사 불가능한 객체들)을 한 컨테이너에서 다른 컨테이너로 이동합니다.

#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));
}

출력:

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

참고 항목

요소 범위를 역순으로 새 위치로 이동
(알고리즘 함수 객체)
요소 범위를 새 위치로 복사
(알고리즘 함수 객체)
요소 범위를 역순으로 복사
(알고리즘 함수 객체)
(C++11)
요소 범위를 새 위치로 이동
(함수 템플릿)
(C++11)
인수를 xvalue로 변환
(함수 템플릿)