std::ranges::copy_backward

cppreference.com

Example

print_rng: (rem: std::string_view, rng: _ is std::ranges::forward_range)
= {
    std::cout << "(rem)$: ";
    for rng do (elem) {
        std::cout << "(elem)$ ";
    }
    std::cout << '\n';
}

main: () = {
    src: std::array = (1, 2, 3, 4);
    print_rng("src", src);

    dst:= std::vector<int>(src.size() + 2);
    std::ranges::copy_backward(src, dst.end());
    print_rng("dst", dst);

    std::ranges::fill(dst, 0);
    res:= std::ranges::copy_backward(src.begin(), src.end() - 2, dst.end());
    print_rng("dst", dst);

    std::cout
        << "(in - src.begin) == " << std::distance(src.begin(), res.in) << '\n'
        << "(out - dst.begin) == " << std::distance(dst.begin(), res.out) << '\n';
}

Output

src: 1 2 3 4 
dst: 0 0 1 2 3 4 
dst: 0 0 0 0 1 2 
(in - src.begin) == 2
(out - dst.begin) == 4

Back to top

cpp2reference.com licensed under CC-BY-SA and GFDL.