Namespaces
Variants

std::ranges:: copy_n, std::ranges:: copy_n_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:: weakly_incrementable O >

requires std:: indirectly_copyable < I, O >
constexpr copy_n_result < I, O >

copy_n ( I first, std:: iter_difference_t < I > n, O result ) ;
(1) (C++20 이후)
헬퍼 타입
template < class I, class O >
using copy_n_result = ranges:: in_out_result < I, O > ;
(2) (C++20 이후)
1) first 에서 시작하는 범위로부터 정확히 n 개의 값을 result 에서 시작하는 범위로 복사합니다. 각 정수 [ 0 , n ) 에 대해 * ( result + i ) = * ( first + i ) 연산을 수행합니다. result 가 범위 [ first , first + n ) 내에 있을 경우 동작은 정의되지 않습니다 (이 경우 ranges::copy_backward 를 대신 사용할 수 있습니다).

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

목차

매개변수

first - 복사할 요소 범위의 시작점
n - 복사할 요소의 개수
result - 대상 범위의 시작점

반환값

ranges :: copy_n_result { first + n, result + n } 또는 더 공식적으로는, ranges::in_out_result 타입의 값으로, input_iterator 반복자를 ranges:: next ( first, n ) 와 동일하게 포함하고, weakly_incrementable 반복자를 ranges:: next ( result, n ) 와 동일하게 포함합니다.

복잡도

정확히 n 번의 할당.

참고 사항

실제로, std::ranges::copy_n 구현은 값 타입이 TriviallyCopyable 이고 반복자 타입이 contiguous_iterator 를 만족하는 경우, 다중 할당을 피하고 std::memmove 와 같은 벌크 복사 함수를 사용할 수 있습니다. 또는 이러한 복사 가속화는 컴파일러의 최적화 단계에서 주입될 수 있습니다.

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

가능한 구현

struct copy_n_fn
{
    template<std::input_iterator I, std::weakly_incrementable O>
    requires std::indirectly_copyable<I, O>
    constexpr ranges::copy_n_result<I, O>
        operator()(I first, std::iter_difference_t<I> n, O result) const
    {
        for (; n-- > 0; (void)++first, (void)++result)
            *result = *first;
        return {std::move(first), std::move(result)};
    }
};
inline constexpr copy_n_fn copy_n{};

예제

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>
#include <string_view>
int main()
{
    const std::string_view in{"ABCDEFGH"};
    std::string out;
    std::ranges::copy_n(in.begin(), 4, std::back_inserter(out));
    std::cout << std::quoted(out) << '\n';
    out = "abcdefgh";
    const auto res{std::ranges::copy_n(in.begin(), 5, out.begin())};
    const auto i{std::distance(std::begin(in), res.in)};
    const auto j{std::distance(std::begin(out), res.out)};
    std::cout << "in[" << i << "] = '" << in[i] << "'\n"
              << "out[" << j << "] = '" << out[j] << "'\n";
}

출력:

"ABCD"
in[5] = 'F'
out[5] = 'f'

참고 항목

요소 범위를 새로운 위치로 복사합니다
(알고리즘 함수 객체)
요소 범위를 역순으로 복사합니다
(알고리즘 함수 객체)
특정 조건을 만족하는 요소를 생략하고 요소 범위를 복사합니다
(알고리즘 함수 객체)
특정 조건을 만족하는 요소를 다른 값으로 대체하여 범위를 복사합니다
(알고리즘 함수 객체)
역순으로 된 범위의 복사본을 생성합니다
(알고리즘 함수 객체)
요소 범위를 복사하고 회전시킵니다
(알고리즘 함수 객체)
연속된 중복 요소가 없는 일부 범위의 복사본을 생성합니다
(알고리즘 함수 객체)
요소 범위를 새로운 위치로 이동합니다
(알고리즘 함수 객체)
요소 범위를 역순으로 새로운 위치로 이동합니다
(알고리즘 함수 객체)
(C++11)
지정된 개수의 요소를 새로운 위치로 복사합니다
(함수 템플릿)