一,STL的适配器
1,bind2nd语法的介绍:
- bind2nd() 函数, bind1sd函数
- 继承binary_function<参数1, 参数2,返回值类型 >写一个 伪函数要用const修饰
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std; //使用 标准的命名空间
#include <functional>
#include <iterator>
class Person
{
public:
string m_Name;
int m_age;
Person(string name, int age) :m_Name(name), m_age(age) {}
bool showclassint(Person p)
{
return this->m_age > p.m_age;
}
};
class myComple :public binary_function<int, int, void>
{
public:
void operator()(int val, int score) const
{
cout << val + score << endl;
}
};
class Mycom
{
public:
bool showclassint(int val1, int val2)
{
return val1 > val2;
}
};
bool showint(int val1, int val2)
{
return val1 > val2;
}
class mynot2 : public unary_function<int, void>
{
public:
bool operator()(int val) const
{
return val < 5;
}
};
void test01()
{
vector<int> v;
v.push_back(31);
v.push_back(233);
v.push_back(3232);
v.push_back(5);
//for_each(v.begin(), v.end(), not1(mynot2()));
//for_each(v.begin(), v.end(), bind2nd(myComple(), 1000000));
//sort(v.begin(), v.end(), showint);
//sort(v.begin(), v.end(), mem_fun_ref(&Mycom::showclassint));
//copy(v.begin(), v.end(), ostream_iterator<int>(cout << " "));
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
void test05()
{
vector<Person> v;
v.push_back(Person("陈丽", 22));
v.push_back(Person("王盼盼", 23));
v.push_back(Person("王蓉", 21));
v.push_back(Person("巍盼盼", 20));
v.push_back(Person("杨艳", 25));
sort(v.begin(), v.end(), mem_fun_ref(&Person::showclassint));
for_each(v.begin(), v.end(), [](Person p) { cout << p.m_Name << " "; });
}
int main(void)
{
//test01();
test05();
system("pause");
return 0;
}
取反适配器的使用not1,not2的使用要配合unary_function<参数类型, 返回值类型>接口使用
列子:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std; //使用 标准的命名空间
//1,适配器的继承 binary_function类
class myCompare : public binary_function<int, int, void>
{
public:
void operator()(int val, int score) const
{
cout << val + score << " ";
}
};
//2,取反适配器的使用
class myFUNC : public unary_function<int, bool>
{
public:
bool operator()(int val1) const
{
return (val1 > 5);
}
};
//class myFUNC2 : public unary_function<int, int, bool>
//{
//public:
// bool operator()(int val1, int socre) const
// {
// return (val1 > socre);
// }
//};
void test01()
{
vector<int> v;
v.push_back(3);
v.push_back(33);
v.push_back(34);
v.push_back(35);
v.push_back(32);
cout << "请输入的基值" << endl;
int score;
cin >> score;
//绑定适配器 bind2nd
//for_each(v.begin(), v.end(), bind2nd(myCompare(), score));
for_each(v.begin(), v.end(), bind1st(myCompare(), score));
cout << endl;
vector<int>::iterator pos = find_if(v.begin(), v.end(), not1(myFUNC()));
pos = find_if(v.begin(), v.end(), not1(bind2nd(greater<int>(), 5)));
if (pos != v.end())
{
cout << "pos" << endl;
}
}
void myfunc(int val, int socre)
{
cout << val + socre << " ";
cout << endl;
}
void test02()
{
vector<int> v;
v.push_back(3);
v.push_back(33);
v.push_back(34);
v.push_back(35);
v.push_back(32);
for_each(v.begin(), v.end(), bind1st(ptr_fun (myfunc), 5));
}
class Person
{
public:
string name;
int age;
Person(string name, int age) :name(name), age(age) {}
void func()
{
cout << name << age + 5 << " ";
cout << endl;
}
};
void test03()
{
vector<Person> v;
v.push_back(Person("陈丽", 23));
v.push_back(Person("王盼盼", 22));
v.push_back(Person("王蓉", 13));
//使用成员函数要加上mem_fun_ref的
for_each(v.begin(), v.end(), mem_fun_ref(&Person::func));
}
int main(void)
{
//test01();
//test02();
test03();
system("pause");
return 0;
}