bind的使用和场景
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <set>
#include <deque>
using namespace std;
template<int socre>
void AddVal(int val)
{
cout << val << " || " << socre << endl;
}
class AddVal2
{
int val;
public:
AddVal2(int j) :val(j) {}
void operator()(int j)
{
cout << j << " || " << val << endl;
}
};
void test01()
{
vector<int> v;
int i;
for (i = 0; i < 10; i++)
v.push_back(i);
for_each(v.begin(), v.end(), AddVal<5>);
for_each(v.begin(), v.end(), AddVal2(5));
}
void test05()
{
set<int> s;
s.insert(4);
s.insert(434);
s.insert(34);
s.insert(24);
s.insert(14);
vector<int> v;
transform(s.begin(), s.end(),
back_inserter(v),
bind(multiplies<int>(), placeholders::_1, 20));
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
void test03()
{
set<int> s;
s.insert(4);
s.insert(434);
s.insert(34);
s.insert(24);
s.insert(14);
deque<int> v;
transform(s.begin(), s.end(),
back_inserter(v),
bind(logical_or<bool>(),
bind(greater<int>(), placeholders::_1, 20),
bind(less<int>(), placeholders::_1, 5))
);
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
int main(void)
{
test03();
system("pause");
return 0;
}