C++ Program to Demonstrate Unary Negater With logical_not Predicate

«
»
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.

  1. /*
  2.  * C++ Program to use logical_not with unary negater
  3.  */
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <functional>
  8. #include <iomanip>
  9. using namespace std;
  10.  
  11. void print(const vector <int>& v)
  12. {
  13.     vector <int>::const_iterator i;
  14.     for (i = v.begin(); i != v.end(); i++)
  15.     {
  16.         cout << setw(2) <<  *i << " ";
  17.     }
  18.     cout << endl;
  19. }
  20.  
  21. int main()
  22. {
  23.     int arr1[] = {0, 1, 1, 0, 1, 0, 0};
  24.     vector <int> v(arr1 , arr1 + sizeof(arr1) / sizeof(int));
  25.  
  26.     cout << "Vector : ";
  27.     print(v);
  28.     cout << "Using logical_not predicate without not1" << endl;
  29.     transform(v.begin(), v.end(), v.begin(), logical_not<int>());
  30.     cout << "Vector : ";
  31.     print(v);
  32.     cout << "Using logical_not predicate with not1" << endl;
  33.     transform(v.begin(), v.end(), v.begin(), not1(logical_not<int>()));
  34.     cout << "Vector : ";
  35.     print(v);
  36. }

$ 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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.