example2_lambda_functions.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    
    std::function<bool(int)> isEven = [](int n) { return n % 2 == 0; };
    
    auto count = std::count_if(numbers.begin(), numbers.end(), isEven);
    
    std::cout << "Number of even integers: " << count << std::endl;
    return 0;
}
Back to std_function