greater_equal in C++

This C++ program implements the greater_equal predicate. The greater_equal predicate is a binary function object which takes two parameters and returns true if first parameter is greater than or equal to the second parameter. We are using this predicate to compare the vectors lexicographically by using lexicographical_compare algorithm and passing greater_equal as the fifth parameter.

Here is the source code of the C++ program which implements the greater_equal 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 implement the greater_equal predicate
  3.  */
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <functional>
  8.  
  9. template<class T>
  10. struct greater_equal : public std::binary_function<T, T, bool> {
  11.     bool operator() (const T& a, const T& b) const
  12.     {
  13.         return (a >= b);
  14.     }
  15. };
  16.  
  17. void print(const std::vector <int>& v)
  18. {
  19.     std::vector <int>::const_iterator i;
  20.     for(i = v.begin(); i != v.end(); i++)
  21.     {
  22.         std::cout << *i << " ";
  23.     }
  24.     std::cout << std::endl;
  25. }
  26.  
  27. int main()
  28. {
  29.     std::vector <int> a(10), b(10);
  30.     greater_equal<int> x;
  31.     bool result;
  32.  
  33.     for (int i = 0; i < 10 ;i++)
  34.     {
  35.         a[i] = i + 1;
  36.         b[i] = i + 1;
  37.     }
  38.     std::cout << "a : ";
  39.     print(a);
  40.     std::cout << "b : ";
  41.     print(b);
  42.     // Comparing a and b lexicographically
  43.     result = std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), x);
  44.     if(result == true)
  45.     {
  46.         std::cout << "a is lexicographically greater than or equal to b."
  47.                   << std::endl;
  48.     }
  49.     else
  50.     {
  51.         std::cout << "a is lexicographically smaller than or equal to b."
  52.                   << std::endl;
  53.     }
  54. }

$ a.out
a : 0 1 2 3 4 5 6 7 8 9 
b : 0 1 2 3 4 5 6 7 8 9 
a is lexicographically greater than or equal to b.

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

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.