C++ Program to Demonstrate Usage of bind1st Binder

This C++ program demonstrates the usage of bind1st binder. The program binds the first parameter of the binary predicate greater and less to 7. A vector of integers is instantiated and whether there is an element greater than 7 and any element equal to 7 is determined.

Here is the source code of the C++ program which demonstrates the usage of bind1st binder. 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 usage of bind1st binder 
  3.  */
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <functional>
  7. #include <vector>
  8.  
  9. using std::cout;
  10. using std::endl;
  11. typedef std::vector <int>::const_iterator const_iterator;
  12.  
  13. void print(const std::vector <int>& v)
  14. {
  15.     const_iterator i;
  16.     for(i = v.begin(); i != v.end(); i++)
  17.     {
  18.         std::cout << *i << " ";
  19.     }
  20.     std::cout << std::endl;
  21. }
  22.  
  23. int main()
  24. {
  25.     std::vector <int> v(10);
  26.     const_iterator p;
  27.  
  28.     for(int i = 0; i < 10; i++)
  29.     {
  30.         v[i] = i % 8;
  31.     }
  32.     cout << "Vector : ";
  33.     print(v);
  34.     p = find_if(v.begin(), v.end(), std::bind1st(std::greater <int>(), 7));
  35.     if (p != v.end())
  36.     {
  37.         std::cout << "Element less than or equal to 7 => "
  38.                   << *p << " at position " << p - v.begin() + 1
  39.                   << std::endl;
  40.     }
  41.     else
  42.     {
  43.         std::cout << "No element less than or equal to 7"
  44.                   << std::endl;
  45.     }
  46.     p = find_if(v.begin(), v.end(), std::bind1st(std::less <int>(), 7));
  47.     if (p != v.end())
  48.     {
  49.         std::cout << "Element greater than or equal to 7 => "
  50.                   << *p << " at position " << p - v.begin() + 1
  51.                   << std::endl;
  52.     }
  53.     else
  54.     {
  55.         std::cout << "No element greater than or equal to 7"
  56.                   << std::endl;
  57.     }
  58. }

$ a.out
Vector : 0 1 2 3 4 5 6 7 0 1 
Element less than or equal to 7 => 0 at position 1
No element greater than or equal to 7

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

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

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.