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.
/*
* C++ program to demonstrate usage of bind1st 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++)
{
std::cout << *i << " ";
}
std::cout << std::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::bind1st(std::greater <int>(), 7));
if (p != v.end())
{
std::cout << "Element less than or equal to 7 => "
<< *p << " at position " << p - v.begin() + 1
<< std::endl;
}
else
{
std::cout << "No element less than or equal to 7"
<< std::endl;
}
p = find_if(v.begin(), v.end(), std::bind1st(std::less <int>(), 7));
if (p != v.end())
{
std::cout << "Element greater than or equal to 7 => "
<< *p << " at position " << p - v.begin() + 1
<< std::endl;
}
else
{
std::cout << "No element greater than or equal to 7"
<< std::endl;
}
}
$ 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.
Related Posts:
- Apply for C++ Internship
- Check C++ Books
- Check Computer Science Books
- Practice Programming MCQs
- Practice Computer Science MCQs