Beginner's Guide

# 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

``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {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) { std::cout << x << ' '; }  // 4 5 6 7 8
std::cout << '\n';
//  UNDEFINED BEHAVIOR: target size is too small!
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {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) { std::cout << x << ' '; }  // 4 5 6 7 8
std::cout << '\n';
}``````

## `copy_n`copy_nC++11

``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {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) { std::cout << x << ' '; }  // 4 5 6 7 8
std::cout << '\n';
}``````

## `copy_backward`copy_backward

``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {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) { std::cout << x << ' '; }  // 4 5 6 7 8
std::cout << '\n';
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {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) { std::cout << x << ' '; }  // 4 5 6 7 8
std::cout << '\n';
}``````

## `copy_if`copy_ifC++11

``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {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
std::cout << "copy_count: " << copy_count << '\n';// resize to fit
tgt.erase(e, end(tgt));
for (int x : tgt) { std::cout << x << ' '; }  // 4 6 8
std::cout << '\n';
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {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
std::cout << "copy_count: " << copy_count << '\n';// resize to fit
tgt.erase(e.out, end(tgt));
for (int x : tgt) { std::cout << x << ' '; }  // 4 6 8
std::cout << '\n';
}``````

## `sample`sampleC++17

``````#include <vector>
#include <iostream>
#include <algorithm>#include <random>

int main () {// 32 bit mersenne twister engine
auto const seed = std::random_device{}();
auto reng = std::mt19937{seed};
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) { std::cout << x <<' '; }  // 1 4 6 8
std::cout << '\n';
}``````
``````#include <vector>
#include <iostream>
#include <algorithm>#include <random>

int main () {// 32 bit mersenne twister engine
auto const seed = std::random_device{}();
auto reng = std::mt19937{seed};
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) { std::cout << x <<' '; }  // 1 4 6 8
std::cout << '\n';
}``````