std::ranges...py_n_result(3)
π NAME π
okay so bestie, welcome to std::ranges::copy_n, std::ranges::copy_n_result β page std::ranges...py_n_result(3) from the C++ Standard Libary manual. Yes, the whole icon of this page is literally std::ranges::copy_n,std::ranges::copy_n_result, and we are going to get into it. π
πΈ Synopsis πΈ
Defined in header <algorithm>
Call signature
template< std::input_iterator I, std::weakly_incrementable O >
requires std::indirectly_copyable<I, O> (1)
(since C++20)
constexpr copy_n_result<I, O>
copy_n( I first, std::iter_difference_t<I> n, O result );
Helper type
template< class I, class O > (2)
(since C++20)
using copy_n_result = ranges::in_out_result<I, O>;
copy_nbehavior: copies exactlynvalues from the range beginning atfirstto the range beginning atresultby performing*(result + i) = *(first + i)for each integer in[0, n). The behavior is undefined ifresultis within the range[first, first + n)β and yeah,ranges::copy_backwardmight be used instead in this case.
π¨ bestie, that undefined behavior is real. Do not play with overlapping ranges like that unless you know exactly what you're doing. ranges::copy_backward is the backup dancer for a reason.
The function-like entities described on this page are niebloids, that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions. β¨
π 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 π¦
ranges::copy_n_result{first + n, result + n} β or more formally, a value 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. no cap, that's the whole count. π
π 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. Alternatively, 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 π
// Run this code β go off:
#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());
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 π
ranges::copyβ copies a range of elements to a new location(C++20)(niebloid)ranges::copy_ifβ copies a range of elements to a new location(C++20)(niebloid)ranges::copy_backwardβ copies a range of elements in backwards order(C++20)(niebloid)ranges::remove_copyβ copies a range of elements omitting those that satisfy specific criteria(C++20)(niebloid)ranges::remove_copy_ifβ copies a range of elements omitting those that satisfy specific criteria(C++20)(niebloid)ranges::replace_copyβ copies a range, replacing elements satisfying specific criteria with another value(C++20)(niebloid)ranges::replace_copy_ifβ copies a range, replacing elements satisfying specific criteria with another value(C++20)(niebloid)ranges::reverse_copyβ creates a copy of a range that is reversed(C++20)(niebloid)ranges::rotate_copyβ copies and rotate a range of elements(C++20)(niebloid)ranges::unique_copyβ creates a copy of some range of elements that contains no consecutive duplicates(C++20)(niebloid)ranges::moveβ moves a range of elements to a new location(C++20)(niebloid)ranges::move_backwardβ moves a range of elements to a new location in backwards order(C++20)(niebloid)copy_nβ copies a number of elements to a new location(C++11)(function template)
source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::ranges...py_n_result(3) β mwah, bestie ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms