Standard Library Range Copy AlgorithmsRange Copy AlgorithmsRange Copy
New to C++'s standard library algorithms? ⇒ Short Introduction
Target ranges must be able to recieve all copied elements!
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!
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
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
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
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
#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
Related …
- Standard Algorithms Overview
- Standard Algorithms Introduction
- Standard Sequence Containers (
vector
,deque
,list
, …) - Standard Associative Containers (
map
,set
, …) - Standard Sequence Views
- cppreference: Algorithms Library
- cppreference: Containers Library
- A Tour of C++: Containers and Algorithms
- Algorithms Overview Sheet