C++ Program to Demonstrate Usage of bind2nd Binder

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

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

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.