Demonstrate bind2nd()
#include <iostream>
#include <list>
#include <functional>
#include <algorithm>
using namespace std;
int main()
{
list<int> lst;
list<int>::iterator p, endp;
for(int i=1; i < 20; i++) lst.push_back(i);
cout << "Original sequence:\n";
p = lst.begin();
while(p != lst.end()) {
cout << *p << " ";
p++;
}
cout << endl;
endp = remove_if(lst.begin(), lst.end(),
bind2nd(greater<int>(), 8));
cout << "Resulting sequence:\n";
p = lst.begin();
while(p != endp) {
cout << *p << " ";
p++;
}
return 0;
}
Original sequence:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Resulting sequence:
1 2 3 4 5 6 7 8
replace_if bind2nd and greater
#include <algorithm>
#include <functional>
#include <vector>
#include <iostream>
using namespace std;
void print(int elem)
{
cout << elem << " ";
}
int main(int argc, char** argv)
{
vector<int> myVector;
myVector.push_back(1);
myVector.push_back(2);
myVector.push_back(3);
myVector.push_back(4);
myVector.push_back(5);
myVector.push_back(6);
replace_if(myVector.begin(), myVector.end(), bind2nd(greater<int>(), 100),100);
for_each(myVector.begin(), myVector.end(), &print);
cout << endl;
return (0);
}
replace_if, bind2nd and less
#include <algorithm>
#include <functional>
#include <vector>
#include <iostream>
using namespace std;
void print(int elem)
{
cout << elem << " ";
}
int main(int argc, char** argv)
{
vector<int> myVector;
myVector.push_back(1);
myVector.push_back(2);
myVector.push_back(3);
myVector.push_back(4);
myVector.push_back(5);
myVector.push_back(6);
replace_if(myVector.begin(), myVector.end(), bind2nd(less<int>(), 0), 0);
replace_if(myVector.begin(), myVector.end(), bind2nd(greater<int>(), 100),100);
for_each(myVector.begin(), myVector.end(), &print);
cout << endl;
return (0);
}
trasform, bind2nd and plus
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
void print(int elem)
{
cout << elem << " ";
}
int main(int argc, char** argv)
{
vector<int> myVector;
myVector.push_back(1);
myVector.push_back(2);
myVector.push_back(3);
myVector.push_back(4);
for_each(myVector.begin(), myVector.end(), &print);
transform(myVector.begin(), myVector.end(), myVector.begin(),bind2nd(plus<int>(), 100));
for_each(myVector.begin(), myVector.end(), &print);
cout << endl;
return (0);
}
Use bind2nd() to create a unary function object that will return true when a value is greater than 10.
#include <iostream>
#include <list>
#include <functional>
#include <algorithm>
using namespace std;
int main()
{
list<int> list1;
list<int>::iterator res_itr;
for(unsigned i=1; i < 20; ++i) list1.push_back(i);
res_itr = remove_if(list1.begin(), list1.end(),bind2nd(greater<int>(), 10));
return 0;
}
Use the generic count algorithm with predicate: Determine the number of array elements that are not equal to 1
#include <iostream>
#include <cassert>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
int a[] = {0, 0, 0, 1, 1, 1, 2, 2, 2};
int final_count = count_if(&a[0], &a[9], bind2nd(not_equal_to<int>(), 1));
cout << final_count << endl;
return 0;
}
6