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 AlgorithmsRange Copy AlgorithmsRange 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

    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!
    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

    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

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

    copy_if copy_if

    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(begin(src)+2, begin(src)+7, begin(tgt), is_even)
    // how many elements copied?
    auto const copy_count = distance(begin(tgt), e);  // 3
    tgt.resize(copy_count);
    for(int x : tgt) { cout << x << ' '; }  // 4 6 8
    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(src, begin(tgt), is_even)
    // how many elements copied?
    auto const copy_count = distance(begin(tgt), e);  // 3
    tgt.resize(copy_count);
    for(int x : tgt) { cout << x << ' '; }  // 4 6 8

    sample sample

    #include <random>
    // 32 bit mersenne twister engine
    auto rgen = 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, rgen);
    for(int x : samples) { cout << x <<' '; }  // 1 4 6 8
    #include <random>
    // 32 bit mersenne twister engine
    auto rgen = 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, rgen);
    for(int x : samples) { cout << x <<' '; }  // 1 4 6 8