Beginner's Guide
    First Steps
    Input & Output
    Custom Types – Part 1
    Diagnostics
    Standard Library – Part 1
    Function Objects
    Standard Library – Part 2
    Code Organization
    Custom Types – Part 2
    Generic Programming
    Memory Management
    Software Design Basics

    Standard Library Range Copy Algorithms Range Copy Algorithms Range Copy

    This means that, e.g., target containers must be resized properly.

    Standard algorithms don't – and in most cases can't – check if the target range is large enough.

    Trying to copy elements beyond the target's capacity will invoke undefined behavior!

    copy copy

    standard library algorithm 'copy' visual example
    std::vector<int> src {1,2,3,4,5,6,7,8,9,0};
    std::vector<int> tgt;
    int const n = 5;
    tgt.resize(n);
    copy(begin(src)+3, begin(src)+8, begin(tgt));
    for(int x : tgt) { cout << x << ' '; }  // 4 5 6 7 8
    
    std::vector<int> bad; bad.resize(2); copy(begin(src), end(src), begin(bad)); // UNDEFINED BEHAVIOR: target size is too small!
    standard library algorithm 'ranges-copy' visual example
    std::vector<int> src {4,5,6,7,8};
    std::vector<int> tgt;
    tgt.resize(src.size());
    std::ranges::copy(src, begin(tgt));
    for(int x : tgt) { cout << x << ' '; }  // 4 5 6 7 8

    copy_n copy_n

    standard library algorithm 'copy_n' visual example
    std::vector<int> src {1,2,3,4,5,6,7,8,9,0};
    std::vector<int> tgt;
    int const n = 5;
    tgt.resize(n);
    copy_n(begin(src)+3, n, begin(tgt));
    for(int x : tgt) { cout << x << ' '; }  // 4 5 6 7 8

    copy_backward copy_backward

    standard library algorithm 'copy_backward' visual example
    std::vector<int> src {1,2,3,4,5,6,7,8,9,0};
    std::vector<int> tgt;
    tgt.resize(5);
    copy_backward(begin(src)+3, begin(src)+8, end(tgt));
    for(int x : tgt) { cout << x << ' '; }  // 4 5 6 7 8
    standard library algorithm 'ranges-copy_backward' visual example
    std::vector<int> src {4,5,6,7,8};
    std::vector<int> tgt;
    tgt.resize(src.size());
    std::ranges::copy_backward(src, end(tgt));
    for(int x : tgt) { cout << x << ' '; }  // 4 5 6 7 8

    copy_if copy_if

    standard library algorithm 'copy_if' visual example
    auto const is_even = [](int x) {   return !(x & 1); };
    std::vector<int> src {2,3,4,5,6,8,9,3};
    std::vector<int> tgt;
    tgt.resize(5);
    auto e = copy_if(begin(src)+2, begin(src)+7, begin(tgt), is_even);
    // how many elements copied?
    auto const copy_count = distance(begin(tgt), e);  // 3
    // resize to fit
    tgt.erase(e, end(tgt));
    for(int x : tgt) { cout << x << ' '; }  // 4 6 8
    standard library algorithm 'ranges-copy_if' visual example
    auto const is_even = [](int x) {   return !(x & 1); };
    std::vector<int> src {4,5,6,8,9};
    std::vector<int> tgt;
    tgt.resize(5);
    auto e = std::ranges::copy_if(src, begin(tgt), is_even);
    // how many elements copied?
    auto const copy_count = distance(begin(tgt), e.out);  // 3
    // resize to fit
    tgt.erase(e.out, end(tgt));
    for(int x : tgt) { cout << x << ' '; }  // 4 6 8

    sample sample

    standard library algorithm 'sample' visual example
    #include <random>
    // 32 bit mersenne twister engine
    auto reng = std::mt19937{};
    int const n = 4;
    std::vector<int> samples;
    samples.resize(n);
    std::vector<int> pop {1,2,4,6,8,9};
    sample(begin(pop), end(pop), begin(samples), n, reng);
    for(int x : samples) { cout << x <<' '; }  // 1 4 6 8
    standard library algorithm 'ranges-sample' visual example
    #include <random>
    // 32 bit mersenne twister engine
    auto reng = std::mt19937{};
    int const n = 4;
    std::vector<int> samples;
    samples.resize(n);
    std::vector<int> pop {1,2,4,6,8,9};
    std::ranges::sample(pop, begin(samples), n, reng);
    for(int x : samples) { cout << x <<' '; }  // 1 4 6 8