bind1st,bind2nd:將二元函數(shù)對(duì)象配置為一元函數(shù)對(duì)象
bind1st 和 bind2nd 區(qū)別: bind1st是把傳入?yún)?shù)綁定第一個(gè)參數(shù)杀狡,bind2nd 是把傳入?yún)?shù)綁定第二個(gè)參數(shù)
?
not1 not2:取反
not1 和 not2的區(qū)別:not1是針對(duì)一元函數(shù)對(duì)象屡拨, not2是針對(duì)二元函數(shù)對(duì)象
?
ptr_fun 將普通函數(shù)轉(zhuǎn)變?yōu)楹瘮?shù)對(duì)象
?
mem_fun、mem_fun_ref 將成員函數(shù)轉(zhuǎn)變?yōu)楹瘮?shù)對(duì)象
1. bind1st,bind2nd
- 第一步:繼承binary_function<參數(shù)1,參數(shù)2绎橘,返回>
- 第二步:void operator()(int v1, int v2) 加上const成為常函數(shù)
- 第三步:實(shí)現(xiàn)函數(shù)體
- 第四步:用bind1st 袍辞、bind2nd 綁定函數(shù)對(duì)象
// 第一步:繼承binary_function<參數(shù)1,參數(shù)2趴乡,返回>
struct MyFunc2 : public binary_function<int, int, void>
{
void operator()(int v1, int v2) const // 第二步:加上const成為常函數(shù)
{
cout << "v1 = " << v1 << endl;
cout << "v2 = " << v2 << endl;
cout << v1 + v2 << " " << endl; // 第三步 實(shí)現(xiàn)函數(shù)體
}
};
void test02()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
// 第四步 用bind2nd 綁定函數(shù)對(duì)象
for_each(v.begin(), v.end(), bind2nd(MyFunc2(), 100)); // MyFunc2() 匿名對(duì)象
cout << endl;
cout << "--------------------" << endl;
for_each(v.begin(), v.end(), bind1st(MyFunc2(), 100)); // MyFunc2() 匿名對(duì)象
cout << endl;
// bind1st 和 bind2nd 區(qū)別
// bind1st是把100 綁定第一個(gè)參數(shù) bind2nd是把100綁定第二個(gè)參數(shù)
}
2. not1 , not2
- 第一步:繼承unary_function<參數(shù)荐吉,返回>
- 第二步:bool operator()(int v) 加上const 成為常函數(shù)
- 第三步:實(shí)現(xiàn)函數(shù)體
- 第四步:not1焙糟、not2 適配 取反
// 第一步:繼承unary_function<參數(shù),返回>
struct MyNotFunc : public unary_function<int, bool>
{
bool operator()(int v) const // 第二步 加上const 成為常函數(shù)
{
return v >= 20; // 第三步 實(shí)現(xiàn)函數(shù)體
}
};
// not1 使用
void test03()
{
vector<int> v;
v.push_back(10);
v.push_back(50);
v.push_back(30);
v.push_back(40);
v.push_back(20);
// find_if
/*
for (; __first != __last; ++__first)
if (__pred(*__first))
break;
return __first;
*/
vector<int>::iterator it = find_if(v.begin(), v.end(), MyNotFunc()); // MyNotFunc() 函數(shù)對(duì)象
if (it == v.end())
{
cout << "查找失敗" << endl;
}
else
{
cout << "查找成功"
<< " " << *it << endl;
}
// 第四步:not1样屠、not2 適配 取反
vector<int>::iterator it1 = find_if(v.begin(), v.end(), not1(MyNotFunc()));
if (it1 == v.end())
{
cout << "查找失敗" << endl;
}
else
{
cout << "查找成功"
<< " " << *it1 << endl;
}
}
// not2的使用
// less 二元函數(shù)對(duì)象
void myPrint(int val)
{
cout << val << " ";
}
void test04()
{
vector<int> v;
v.push_back(10);
v.push_back(50);
v.push_back(30);
v.push_back(40);
v.push_back(20);
sort(v.begin(), v.end(), less<int>());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
cout << "-------------------" << endl;
sort(v.begin(), v.end(), not2(less<int>()));
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
3. 普通函數(shù)適配 ptr_fun
如何給一個(gè)普通函數(shù)使用綁定適配器(bind1st bind2nd)綁定一個(gè)參數(shù)穿撮?
(1) 將普通函數(shù)適配成函數(shù)對(duì)象
(2) 然后通過(guò)綁定器綁定參數(shù)
void myPrint2(int val1, int val2)
{
cout << val1 + val2 << " ";
}
void test05()
{
vector<int> v;
v.push_back(10);
v.push_back(50);
v.push_back(30);
v.push_back(40);
v.push_back(50);
// 第二步: 把普通函數(shù)變?yōu)楹瘮?shù)對(duì)象
for_each(v.begin(), v.end(), bind2nd(ptr_fun(myPrint2), 100));
cout << endl;
}
4.成員函數(shù)適配 mem_fun、mem_fun_ref
mem_fun:如果存儲(chǔ)的是對(duì)象指針痪欲,需要使用mem_fun
mem_fun_ref:如果存儲(chǔ)的是對(duì)象悦穿,需要使用mem_fun_ref
class Maker
{
public:
Maker(string name, int age)
{
this->name = name;
this->age = age;
}
void myPrintMaker()
{
cout << "Name: " << this->name << " Age: " << this->age << endl;
}
bool myGreaterThan(int val2) {
return this->age > val2;
}
public:
string name;
int age;
};
void test06()
{
vector<Maker> v;
v.push_back(Maker("aaa", 10));
v.push_back(Maker("bbb", 20));
v.push_back(Maker("ccc", 30));
// 當(dāng)容器存儲(chǔ)的是對(duì)象,用mem_fun_ref適配它的成員函數(shù) (&取地址符)
for_each(v.begin(), v.end(), mem_fun_ref(&Maker::myPrintMaker));
cout << "--------------------" << endl;
vector<Maker *> vp;
vp.push_back(new Maker("aaa", 10));
vp.push_back(new Maker("bbb", 20));
vp.push_back(new Maker("ccc", 30));
// 當(dāng)容器存儲(chǔ)的是對(duì)象指針业踢,用mem_fun適配它的成員函數(shù)
for_each(vp.begin(), vp.end(), mem_fun(&Maker::myPrintMaker));
cout << "--------------------" << endl;
int num = count_if(vp.begin(),vp.end(),bind2nd(mem_fun(&Maker::myGreaterThan),15));
cout << "age>15" << " " << num << endl;
int num2 = count_if(vp.begin(),vp.end(),not1(bind2nd(mem_fun(&Maker::myGreaterThan),15)));
cout << "age<=15" << " " << num2 << endl;
}