Find vector elements with even values

Could you explain how this code works. It successfully takes into account vector elements with even values, but it is not clear to me how the binding works in this particular case.

count_if(vec.begin(), vec.end(),
         std::bind(logical_not<bool>(),
                   std::bind(modulus<int>(), placeholders::_1, 2)));
+4
source share
4 answers

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() {
    // Test vector
    vector<int> vec{ 11, 22, 33, 44, 55 };

    // Using functional approach
    auto n = count_if(vec.begin(), vec.end(),
         bind(logical_not<bool>(),
             bind(modulus<int>(), placeholders::_1, 2)));    
    cout << n << endl;

    // Using lambdas
    n = count_if(vec.begin(), vec.end(), 
                 [](int n) { return (n % 2) == 0; });
    cout << n << endl;

    // Using boolean returning ad hoc function
    n = count_if(vec.begin(), vec.end(), IsEven);
    cout << n << endl;
}
+2

:

modulus<int>( a, 2 )

- 2: == 0 != 0.

logical_not<bool>( x )

x ( 0/false 1/true 1/true 0/false)

count_if(from, to, cond )

cond ( ) , , /.

placeholders::_1 - - , count_if (.. ) , .

+2

, .

, 2 true, 1.

, (), , operator()

constexpr T operator()( const T& lhs, const T& rhs ) const;

lhs rhs.

std:: bind ,

count_if( InputIt first, InputIt last, UnaryPredicate p )

p ( ) 2

std::bind(modulus<int>(), placeholders::_1, 2))

std::bind(modulus<int>(), placeholders::_1, 2)) true (1), false (0), . , , :

std::bind(logical_not<bool>(),
                           std::bind(modulus<int>(), placeholders::_1, 2))

http://ideone.com/80VmsZ

, -:

[](int x){return x % 2 == 0;} // to count even elements
[](int x){return x % 2 != 0;} // to count odds
+1

, modulus 2 ,

y = x % 2

And then the result is tied to logical_notto cancel the result.

0
source

Source: https://habr.com/ru/post/1537095/


All Articles