C++ std::copy_if() Function

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.
std::copy_if(src.begin(), src.end(), std::back_inserter(dst), pred);
std::copy_if(src.begin(), src.end(), dst.begin(), pred);
| Call | What 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
template<class InputIt, class OutputIt, class UnaryPred>
OutputIt copy_if(InputIt first, InputIt last,
OutputIt d_first, UnaryPred pred);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The source range. |
| d_first | Required. Where the copied elements go. |
| pred | Required. A predicate. Only elements for which it returns true are copied. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points 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.
#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
- The lambda is true for even numbers.
copy_if() visits all eight source values and copies only those that pass.std::back_inserter appends them to evens, keeping their original order.
Example 2 — Keep longer words
Filter strings by their length.
#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
- The rule is "at least four letters".
to and fun have fewer letters, so they are skipped.code and learn pass and are added to the new vector.
Example 3 — Keep positive numbers
Print how many values were kept.
#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
n > 0 is false for zero and negative numbers.- Only 5, 7 and 3 pass, in that order.
- 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.
About the author
Developer, cloud engineer, and technical writer
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