Modifying transform copy_if replace
Transform, filter and replace elements with standard algorithms.
Modifying transform copy_if replace is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Modifying Algorithms
These algorithms write to an output range — either in place or to a destination iterator.
std::copy
Copy a range into another. The output range must be large enough or you must provide an inserter.
#include <algorithm>
#include <iterator>
std::vector<int> src = {1, 2, 3};
std::vector<int> dst;
std::copy(src.begin(), src.end(), std::back_inserter(dst));std::copy_if
Copy elements that match a predicate.
std::vector<int> evens;
std::copy_if(src.begin(), src.end(),
std::back_inserter(evens),
[](int x) { return x % 2 == 0; });std::transform
Apply a function to each element and write the result. Like map in other languages.
std::vector<int> v = {1, 2, 3};
std::vector<int> sq;
std::transform(v.begin(), v.end(),
std::back_inserter(sq),
[](int x) { return x * x; });
// sq is {1, 4, 9}Two-Range transform
Combine two input ranges with a binary function.
std::vector<int> a = {1, 2, 3};
std::vector<int> b = {10, 20, 30};
std::vector<int> sum;
std::transform(a.begin(), a.end(), b.begin(),
std::back_inserter(sum), std::plus<int>{});
// sum is {11, 22, 33}std::replace and std::replace_if
Replace elements equal to a value (or matching a predicate) in place.
std::vector<int> v = {1, 2, 3, 2};
std::replace(v.begin(), v.end(), 2, 99);
// v is {1, 99, 3, 99}std::fill
Set every element of a range to a value.
std::vector<int> v(5);
std::fill(v.begin(), v.end(), 42);
// v is {42, 42, 42, 42, 42}std::generate
Set each element to the result of calling a function. Useful for sequence generation.
std::vector<int> v(5);
int n = 0;
std::generate(v.begin(), v.end(), [&]() { return n++; });
// v is {0, 1, 2, 3, 4}std::iota
From <numeric> — fills a range with sequentially increasing values.
#include <numeric>
std::vector<int> v(5);
std::iota(v.begin(), v.end(), 10);
// v is {10, 11, 12, 13, 14}std::reverse
Reverse the elements of a range in place.
std::vector<int> v = {1, 2, 3, 4};
std::reverse(v.begin(), v.end());
// v is {4, 3, 2, 1}std::rotate
Rotate elements so that the element at middle becomes the new first. The original first goes to the end of the rotated region.
Erase-Remove with std::remove
remove shifts unwanted elements to the back and returns an iterator to the new logical end. Pair with erase to actually shrink.
v.erase(
std::remove(v.begin(), v.end(), 0),
v.end()
);Quick Check
Which algorithm best fits "apply a function to every element and build a new container"?
Recap
Modifying algorithms write results: copy, copy_if, transform, replace, fill, generate, iota, reverse, rotate, and the erase-remove idiom.
Frequently asked questions
Is the “Modifying transform copy_if replace” lesson free?
Yes — the full text of “Modifying transform copy_if replace” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Modifying transform copy_if replace”?
Transform, filter and replace elements with standard algorithms. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Modifying transform copy_if replace” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.