Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/ranges/copy n"

From cppreference.com
< cpp‎ | algorithm‎ | ranges
m (add return type to the page's title)
m (fmt: spaces)
Line 5: Line 5:
 
{{dcl h|Call signature}}
 
{{dcl h|Call signature}}
 
{{dcl | since=c++20 | num=1 |1=
 
{{dcl | since=c++20 | num=1 |1=
template<std::input_iterator I, std::weakly_incrementable O>
+
template< std::input_iterator I, std::weakly_incrementable O >
 
   requires std::indirectly_copyable<I, O>
 
   requires std::indirectly_copyable<I, O>
    constexpr ranges::copy_n_result<I, O>
+
  constexpr ranges::copy_n_result<I, O>
      ranges::copy_n( I first, std::iter_difference_t<I> n, O result );
+
  ranges::copy_n( I first, std::iter_difference_t<I> n, O result );
 
}}
 
}}
 
{{dcl h | Helper type}}
 
{{dcl h | Helper type}}
 
{{dcl | since=c++20 | num=2 |1=
 
{{dcl | since=c++20 | num=2 |1=
template<class I, class O>
+
template< class I, class O >
 
   using copy_n_result = ranges::in_out_result<I, O>;
 
   using copy_n_result = ranges::in_out_result<I, O>;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
@1@ Copies exactly {{tt|n}} values from the range beginning at {{tt|first}} to the range beginning at {{tt|result}}. Effect: for each integer {{tt|0 ≤ i < n}}, performs {{c|1=*(result + i) = *(first + i)}}. The behavior is undefined if {{tt|result}} is within the range {{tt|[first, first + n)}}. Then, the {{c|ranges::copy_backward}} might be used instead.
+
@1@ Copies exactly {{tt|n}} values from the range beginning at {{tt|first}} to the range beginning at {{tt|result}}. Effect: for each integer {{c|1=0 ≤ i < n}}, performs {{c|1=*(result + i) = *(first + i)}}. The behavior is undefined if {{tt|result}} is within the range {{tt|[first, first + n)}}. Then, the {{c|ranges::copy_backward}} might be used instead.
  
 
{{cpp/ranges/niebloid}}
 
{{cpp/ranges/niebloid}}
Line 44: Line 44:
 
   template<std::input_iterator I, std::weakly_incrementable O>
 
   template<std::input_iterator I, std::weakly_incrementable O>
 
     requires std::indirectly_copyable<I, O>
 
     requires std::indirectly_copyable<I, O>
      constexpr ranges::copy_n_result<I, O>
+
    constexpr ranges::copy_n_result<I, O>
        operator()(I first, std::iter_difference_t<I> n, O result) const {
+
    operator()(I first, std::iter_difference_t<I> n, O result) const {
          for (std::iter_difference_t<I> i{}; i != n; ++i, ++first, ++result)
+
        for (std::iter_difference_t<I> i{}; i != n; ++i, ++first, ++result)
 
             *result = *first;
 
             *result = *first;
          return {std::move(first), std::move(result)};
+
        return {std::move(first), std::move(result)};
        }
+
    }
 
};
 
};
  

Revision as of 23:10, 14 June 2021

 
 
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
(C++11)                (C++11)(C++11)

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
(C++11)
(C++17)
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
Numeric operations
(C++23)            
Operations on uninitialized storage
Return types
 
Defined in header <algorithm>
Call signature
template< std::input_iterator I, std::weakly_incrementable O >

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

  ranges::copy_n( I first, std::iter_difference_t<I> n, O result );
(1) (since C++20)
Helper type
template< class I, class O >
  using copy_n_result = ranges::in_out_result<I, O>;
(2) (since C++20)
1) Copies exactly n values from the range beginning at first to the range beginning at result. Effect: for each integer 0 ≤ i < n, performs *(result + i) = *(first + i). The behavior is undefined if result is within the range [first, first + n). Then, the ranges::copy_backward might be used instead.

The function-like entities described on this page are niebloids, that is:

In practice, they may be implemented as function objects, or with special compiler extensions.

Contents

Parameters

first - the beginning of the range of elements to copy from
n - number of the elements to copy
result - the beginning of the destination range

Return value

The ranges::copy_n_result{first + n, result + n} or more formally, an object of type ranges::in_out_result that contains an std::input_iterator iterator equals to ranges::next(first, n) and a std::weakly_incrementable iterator equals to ranges::next(result, n).

Complexity

Exactly n assignments.

Notes

In practice, implementations of std::ranges::copy_n may avoid multiple assignments and use bulk copy functions such as std::memmove if the value type is TriviallyCopyable and the iterator types satisfy contiguous_iterator. Alternativelly, such copy acceleration can be injected during an optimization phase of a compiler.

When copying overlapping ranges, std::ranges::copy_n is appropriate when copying to the left (beginning of the destination range is outside the source range) while std::ranges::copy_backward is appropriate when copying to the right (end of the destination range is outside the source range).

Possible implementation

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 (std::iter_difference_t<I> i{}; i != n; ++i, ++first, ++result)
            *result = *first;
        return {std::move(first), std::move(result)};
    }
};
 
inline constexpr copy_n_fn copy_n{};

Example

#include <algorithm>
#include <iterator>
#include <iostream>
#include <iomanip>
#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());
    std::cout
        << "*(res.in): '" << *(res.in) << "', distance: "
        << std::distance(std::begin(in), res.in) << '\n'
        << "*(res.out): '" << *(res.out) << "', distance: "
        << std::distance(std::begin(out), res.out) << '\n';
}

Output:

"ABCD"
*(res.in): 'F', distance: 5
*(res.out): 'f', distance: 5

See also

copies a range of elements to a new location
(niebloid)[edit]
copies a range of elements in backwards order
(niebloid)[edit]
copies a range of elements omitting those that satisfy specific criteria
(niebloid)[edit]
copies a range, replacing elements satisfying specific criteria with another value
(niebloid)[edit]
creates a copy of a range that is reversed
(niebloid)[edit]
copies and rotate a range of elements
(niebloid)[edit]
creates a copy of some range of elements that contains no consecutive duplicates
(niebloid)[edit]
moves a range of elements to a new location
(niebloid)[edit]
moves a range of elements to a new location in backwards order
(niebloid)[edit]
(C++11)
copies a number of elements to a new location
(function template) [edit]