Welcome! Today, let's explore conditional functions in C++. Conditional functions help us decide and filter data based on conditions, making our code modular and easier to manage.
By the end, you’ll understand how to use std::count_if
, std::copy_if
, and std::replace_if
with predicates in C++. Let’s start by understanding predicates.
A predicate is a function that returns a boolean value, deciding whether a condition holds true.
Consider this function, is_even
, which checks if a number is even:
C++1bool is_even(int n) { 2 return n % 2 == 0; 3}
Here, the function returns true
if n
is even and false
otherwise. This predicate will help us filter or count even numbers in our data. We can also define this predicate as lambda. We will use both versions.
std::count_if
counts elements in a range that meet a condition, which is useful for knowing how many elements fit a criterion.
Here's how to count even numbers in a vector using std::count_if
and is_even
:
C++1#include <iostream> 2#include <vector> 3#include <algorithm> 4 5bool is_even(int n) { 6 return n % 2 == 0; 7} 8 9int main() { 10 std::vector<int> data = {1, 2, 3, 4, 5, 6}; 11 12 // Count even numbers 13 int count = std::count_if(data.begin(), data.end(), is_even); 14 std::cout << "Number of even numbers: " << count << '\n'; // Number of even numbers: 3 15 16 return 0; 17}
std::count_if
goes through data
and counts elements that are even.
std::copy_if
copies elements from a source to a destination if they meet a condition. It’s useful for creating a new container with specific elements.
Here's how to copy even numbers from data
to a new vector, evens
:
C++1#include <iostream> 2#include <vector> 3#include <algorithm> 4#include <iterator> 5 6bool is_even(int n) { 7 return n % 2 == 0; 8} 9 10int main() { 11 std::vector<int> data = {1, 2, 3, 4, 5, 6}; 12 std::vector<int> evens; 13 14 // Copy even numbers to 'evens' 15 std::copy_if(data.begin(), data.end(), std::back_inserter(evens), is_even); 16 17 std::cout << "Even numbers: "; 18 for (int n : evens) { 19 std::cout << n << ' '; 20 } 21 std::cout << '\n'; 22 23 // Output: Even numbers: 2 4 6 24 25 return 0; 26}
std::copy_if
goes through data
and copies even numbers to evens
. std::back_inserter
constructs a special iterator that appends elements to the end of evens
as they are copied by std::copy_if
.
std::replace_if
replaces elements in a range that meet a condition with a new value, which is useful for updating elements in place.
Here's how to replace odd numbers in the data
vector with -1
:
C++1#include <iostream> 2#include <vector> 3#include <algorithm> 4 5int main() { 6 std::vector<int> data = {1, 2, 3, 4, 5, 6}; 7 std::vector<int> replaced_data = data; 8 9 // Replace odd numbers with -1 10 std::replace_if(replaced_data.begin(), replaced_data.end(), [](int n) { 11 return n % 2 != 0; 12 }, -1); 13 14 std::cout << "Replaced data (odds with -1): "; 15 for (int n : replaced_data) { 16 std::cout << n << ' '; 17 } 18 std::cout << '\n'; 19 20 // Output: Replaced data (odds with -1): -1 2 -1 4 -1 6 21 22 return 0; 23}
std::replace_if
replaces odd numbers in replaced_data
with -1
. Here, we use a lambda function, which works the same as our predefined is_even
function.
Today, we explored conditional functions in C++. We learned about predicates and how they can be used with std::count_if
, std::copy_if
, and std::replace_if
for conditional data operations.
To recap:
- Predicates return boolean values.
std::count_if
counts elements meeting a criterion.std::copy_if
copies elements meeting a criterion.std::replace_if
replaces elements meeting a criterion.
Now, it's time to practice using these conditional functions and predicates. Hands-on exercises will help reinforce your understanding and skills. Dive into the exercises to apply what you've learned!