1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
#include <algorithm>
class DivisibleBy {
private:
int divisor;
public:
DivisibleBy(int d) : divisor(d) {}
bool operator()(int x) const {
return x % divisor == 0;
}
};
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int divisor = 3;
DivisibleBy isDivisibleBy3(divisor);
int count = std::count_if(numbers.begin(), numbers.end(), isDivisibleBy3);
std::cout << "Numbers divisible by " << divisor << ": " << count << std::endl;
return 0;
}
Back to functor