C++ std::copy_if() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++11

Definition

In C++, std::copy_if() copies the elements of a range for which a predicate is true. The other elements are skipped.

It is a simple way to filter a container into a new one without writing a loop.

Syntax

Add #include <algorithm>, then call std::copy_if(). Start with these two lines.

C++
std::copy_if(src.begin(), src.end(), std::back_inserter(dst), pred);
std::copy_if(src.begin(), src.end(), dst.begin(), pred);
CallWhat it does
std::copy_if(src.begin(), src.end(), std::back_inserter(dst), pred);Filter into a new vector.
std::copy_if(src.begin(), src.end(), dst.begin(), pred);Filter into a destination that is large enough.
Library declarations
C++
template<class InputIt, class OutputIt, class UnaryPred>
OutputIt copy_if(InputIt first, InputIt last,
                 OutputIt d_first, UnaryPred pred);

Parameters

ParameterDescription
first, lastRequired. The source range.
d_firstRequired. Where the copied elements go.
predRequired. A predicate. Only elements for which it returns true are copied.

Return Value

Return ValueDescription
IteratorPoints just after the last element written in the destination.

C++ Examples

Example 1 — Keep even numbers

Build a new vector with only the even numbers.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> src = {1, 2, 3, 4, 5, 6, 7, 8};
    std::vector<int> evens;

    std::copy_if(src.begin(), src.end(), std::back_inserter(evens),
                 [](int n) { return n % 2 == 0; });

    for (int x : evens) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. The lambda is true for even numbers.
  2. copy_if() visits all eight source values and copies only those that pass.
  3. std::back_inserter appends them to evens, keeping their original order.

Example 2 — Keep longer words

Filter strings by their length.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> words = {"to", "code", "fun", "learn"};
    std::vector<std::string> longWords;

    std::copy_if(words.begin(), words.end(), std::back_inserter(longWords),
                 [](const std::string& w) { return w.size() >= 4; });

    for (const std::string& x : longWords) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. The rule is "at least four letters".
  2. to and fun have fewer letters, so they are skipped.
  3. code and learn pass and are added to the new vector.

Example 3 — Keep positive numbers

Print how many values were kept.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> src = {-2, 5, 0, 7, -9, 3};
    std::vector<int> kept;

    std::copy_if(src.begin(), src.end(), std::back_inserter(kept),
                 [](int n) { return n > 0; });

    std::cout << "Kept " << kept.size() << " of " << src.size() << '\n';
    for (int x : kept) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. n > 0 is false for zero and negative numbers.
  2. Only 5, 7 and 3 pass, in that order.
  3. The source still has six elements, so we can compare the two sizes.

Frequently Asked Questions

In C++, std::copy_if() copies only the elements that satisfy a condition. Include <algorithm> to use it.
Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns an iterator to the position after the last copied element in the destination.
copy_if() builds a new filtered copy and keeps the source. remove_if() shuffles unwanted elements to the end of the same container.
C++11. Before that you had to write a loop.

Next: copy_n()

Copies a fixed number of elements to another place.

copy_n() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

24 people found this page helpful