equal_to Predicate in C++

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

Here is the source code of the C++ program which demonstrates the binary negater with equal_to 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 negater with binary equal_to 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(int a[], int N)
  12. {
  13.     for(int i = 0; i < N; i++)
  14.     {
  15.         cout << setw(2) << a[i] << " ";
  16.     }
  17.     cout << endl;
  18. }
  19.  
  20. int main()
  21. {
  22.     int arr1[] = {0, 1, 2, 4, 8, 16, 32};
  23.     int arr2[] = {0, 1, 2, 4, 8, 16, 32, 64};
  24.     bool result;
  25.  
  26.     cout << "arr1 : ";
  27.     print(arr1, 7);
  28.     cout << "arr2 : ";
  29.     print(arr2, 8);
  30.     // Comparing a and b lexicographically
  31.     cout << "Comparing using negater with equal_to predicate"
  32.          << endl;
  33.     result = lexicographical_compare(arr1, arr1 + 7, arr2 , arr2 + 8, not2(equal_to<int>()));
  34.     if(result == true)
  35.     {
  36.         cout << "arr1 is lexicographically is not equal to arr2."
  37.              << endl;
  38.     }
  39.     else
  40.     {
  41.         cout << "a is lexicographically equal to b."
  42.              << endl;
  43.     }
  44. }

$ a.out
arr1 :  0  1  2  4  8 16 32 
arr2 :  0  1  2  4  8 16 32 64 
Comparing using negater with equal_to predicate
arr1 is lexicographically is not equal to arr2.

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
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.