logical_and Predicate Function in C++

bookmark

#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
 
void print(const vector <int>& v)
{
    vector <int>::const_iterator i;
    for(i = v.begin(); i != v.end(); i++)
    {
        cout << *i << " ";
    }
    cout << endl;
}
 
int main()
{
    vector <int> a(10), b(10);
 
    for (int i = 0; i < 10 ;i++)
    {
        a[i] = i % 2;
        b[i] = i % 4;
    }
    cout << "Vector a : ";
    print(a);
    cout << "Vector b : ";
    print(b);
    // Save the result in vector a
    transform(a.begin(), a.end(), b.begin(), a.begin(), logical_and <int>());
    cout << "a AND b  : ";
    print(a);
}

 

Output:
Vector a : 0 1 0 1 0 1 0 1 0 1 
Vector b : 0 1 2 3 0 1 2 3 0 1 
a AND b  : 0 1 0 1 0 1 0 1 0 1