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.
/*
* C++ program to demonstrate usage of bind2nd binder
*/
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using std::cout;
using std::endl;
typedef std::vector <int>::const_iterator const_iterator;
void print(const std::vector <int>& v)
{
const_iterator i;
for (i = v.begin(); i != v.end(); i++)
{
cout << *i << " ";
}
cout << endl;
}
int main()
{
std::vector <int> v(10);
const_iterator p;
for(int i = 0; i < 10; i++)
{
v[i] = i % 8;
}
cout << "Vector : ";
print(v);
p = find_if(v.begin(), v.end(), std::bind2nd(std::greater <int>(), 7));
if(p != v.end())
{
cout << "Element greater than 7 => " << *p
<< " at position " << p - v.begin() + 1
<< endl;
}
else
cout << "No element greater than 7" << endl;
p = find_if(v.begin(), v.end(), std::bind2nd(std::equal_to <int>(), 7));
if(p != v.end())
{
cout << "Element equal to 7 at position "
<< p - v.begin() + 1 << endl;
}
else
cout << "No element equal to 7" << endl;
}
$ 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
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Practice Computer Science MCQs
- Check Computer Science Books
- Check Programming Books
- Apply for Computer Science Internship
- Practice Programming MCQs