This C++ program demonstrates the unary negater with logical_not predicate. The program creates a vector, computes logical not of all the elements of the vector and also computes negation of logical not of all elements of the vector. The resulting vectors are then printed on the standard output.
Here is the source code of the C++ program which demonstrates the unary negater with logical_not predicate. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to use logical_not with unary negater
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <iomanip>
using namespace std;
void print(const vector <int>& v)
{
vector <int>::const_iterator i;
for (i = v.begin(); i != v.end(); i++)
{
cout << setw(2) << *i << " ";
}
cout << endl;
}
int main()
{
int arr1[] = {0, 1, 1, 0, 1, 0, 0};
vector <int> v(arr1 , arr1 + sizeof(arr1) / sizeof(int));
cout << "Vector : ";
print(v);
cout << "Using logical_not predicate without not1" << endl;
transform(v.begin(), v.end(), v.begin(), logical_not<int>());
cout << "Vector : ";
print(v);
cout << "Using logical_not predicate with not1" << endl;
transform(v.begin(), v.end(), v.begin(), not1(logical_not<int>()));
cout << "Vector : ";
print(v);
}
$ a.out Vector : 0 1 1 0 1 0 0 Using logical_not predicate without not1 Vector : 1 0 0 1 0 1 1 Using logical_not predicate with not1 Vector : 1 0 0 1 0 1 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:
- Apply for Computer Science Internship
- Apply for C++ Internship
- Check C++ Books
- Check Programming Books
- Practice Computer Science MCQs