std::move_backward

cppreference.com

Example

container: type == std::vector<std::string>;

print_cont: (comment: std::string_view, src: container) = {
    print_cont(comment, src, ());
}

print_cont: (comment: std::string_view, src: container, dst: container) = {
    prn:= :(name: std::string_view, cont: container) = {
        std::cout << name;
        for cont do (s) {
            if s.empty() {
                std::cout << "∙ ";
            }
            else {
                std::cout << s.data() << " ";
            }
        }
        std::cout << '\n';
    };
    std::cout << comment << '\n';
    prn("src: ", src);
    if dst.empty() {
        return;
    }
    prn("dst: ", dst);
}

main: () = {
    src: container = ("foo", "bar", "baz");
    dst: container = ("qux", "quux", "quuz", "corge");
    print_cont("Non-overlapping case; before move_backward:", src, dst);
    std::move_backward(src.begin(), src.end(), dst.end());
    print_cont("After:", src, dst);

    src = ("snap", "crackle", "pop", "lock", "drop");
    print_cont("Overlapping case; before move_backward:", src);
    std::move_backward(src.begin(), std::next(src.begin(), 3), src.end());
    print_cont("After:", src);
}

Output

Non-overlapping case; before move_backward:
src: foo bar baz 
dst: qux quux quuz corge 
After:
src: ∙ ∙ ∙ 
dst: qux foo bar baz 
Overlapping case; before move_backward:
src: snap crackle pop lock drop 
After:
src: ∙ ∙ snap crackle pop 

Back to top

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