C++ Academy · Lesson

Modifying transform copy_if replace

Transform, filter and replace elements with standard algorithms.

Lesson 2 of 414 steps

Modifying transform copy_if replace is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Modifying transform copy_if replace” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Modifying transform copy_if replace”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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.

All lessons in this course

  1. Non-Modifying Algorithms find count all_of
  2. Modifying transform copy_if replace
  3. Sorting and Partitioning sort stable_partition
  4. Numeric Algorithms accumulate reduce transform_reduce
← Back to C++ Academy