C++ Program to Demonstrate Binary Negater With less Predicate

«
»
This C++ program demonstrates the binary negater with less predicate. The program creates two vectors and lexicographically compares the two vectors with negated version of less predicate. This results in an inverse effect as without negation.

Here is the source code of the C++ program which demonstrates the binary negater with less 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 demonstrate binary negater with less predicate
  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.     vector <int> a(10), b(10);
  24.     bool result;
  25.  
  26.     for (int i = 0; i < 10 ;i++)
  27.     {
  28.         a[i] = i + 1;
  29.         b[i] = i + 2;
  30.     }
  31.     cout << "a : ";
  32.     print(a);
  33.     cout << "b : ";
  34.     print(b);
  35.     // Comparing a and b lexicographically
  36.     cout << "Comparing using negater with less predicate"
  37.          << endl;
  38.     result = lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), not2(less<int>()));
  39.     if(result == true)
  40.     {
  41.         cout << "a is lexicographically smaller than b."
  42.              << endl;        
  43.     }
  44.     else
  45.     {
  46.         cout << "a is lexicographically greater than or equal to b."
  47.              << endl;
  48.     }
  49. }

$ a.out
a :  1  2  3  4  5  6  7  8  9 10 
b :  2  3  4  5  6  7  8  9 10 11 
Comparing using negater with less predicate
a is lexicographically smaller than b.

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.