Please note that the code you sent takes into account the numbers even in the vector, and not odd ones:
count_if(vec.begin(), vec.end(),
bind(logical_not<bool>(),
bind(modulus<int>(), placeholders::_1, 2)));
count_if is an algorithm that returns the number of elements in a specified range that satisfy certain criteria:
count_if(first, last, criteria)
In your case, firstthere is vec.begin()also lastis vec.end(): therefore the whole vector is counted for count.
Now focus on the criteria part.
:
modulus<int> , ( %). : placeholders::_1, . x, .
- 2, x % 2 0:
x % 2 == 0 --> even number
x % 2 == 1 --> odd number
bind modulus.
: logical_not<bool>. , . false (0), logical_not<bool> true .
, " " :
- :
placeholders::_1 % 2, .. <<generic vector element>> % 2, modulus. 0 (false), true ( ), logical_not.
, :
even number % 2 == 0- 0
true.
, :
odd number % 2 == 1- 1
false.
count_if , true, .
, (.. logical_not):
auto odds = count_if(vec.begin(), vec.end(),
bind(modulus<int>(), placeholders::_1, 2));
, modulus logical_not : ( IsEven()) . < > ( , Ideone) :
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
bool IsEven(int n) {
return (n % 2) == 0;
}
int main() {
vector<int> vec{ 11, 22, 33, 44, 55 };
auto n = count_if(vec.begin(), vec.end(),
bind(logical_not<bool>(),
bind(modulus<int>(), placeholders::_1, 2)));
cout << n << endl;
n = count_if(vec.begin(), vec.end(),
[](int n) { return (n % 2) == 0; });
cout << n << endl;
n = count_if(vec.begin(), vec.end(), IsEven);
cout << n << endl;
}