This C++ program demonstrates the logical_and() algorithm. The program uses transform function from algorithm library to compute the logical_and of corresponding elements of two vectors by passing logical_and as the binary predicate. The result is stored in the first vector itself.
Here is the source code of the C++ program which demonstrates the logical_and() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to demonstrate usage of logical_and predicate
*/
#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);
}
$ a.out 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
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Check Programming Books
- Apply for C++ Internship
- Practice Programming MCQs
- Apply for Information Technology Internship
- Check C++ Books