std::ranges::swap_ranges

cppreference.com

Example

print_cont: (name: std::string_view, seq) = {
    print_cont(name, seq, "\n");
}

print_cont: (name: std::string_view, seq, term: std::string_view) = {
    std::cout << "(name)$: ";
    for seq do (elem) {
        std::cout << "(elem)$ ";
    }
    std::cout << term;
}

main: () = {
    p: std::vector<char> = ('A', 'B', 'C', 'D', 'E');
    q: std::list<char>   = ('1', '2', '3', '4', '5', '6');

    print_cont("p", p);
    print_cont("q", q, "\n\n");

    // swap p[0, 2) and q[1, 3):
    std::ranges::swap_ranges(p.begin(),
                             p.begin() + 4,
                             std::ranges::next(q.begin(), 1),
                             std::ranges::next(q.begin(), 3));
    print_cont("p", p);
    print_cont("q", q, "\n\n");

    // swap p[0, 5) and q[0, 5):
    std::ranges::swap_ranges(p, q);

    print_cont("p", p);
    print_cont("q", q);
}

Output

p: A B C D E 
q: 1 2 3 4 5 6 

p: 2 3 C D E 
q: 1 A B 4 5 6 

p: 1 A B 4 5 
q: 2 3 C D E 6 

Back to top

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