std::greater in C++

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

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

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

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

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.