函数对象适配器
来源:互联网 发布:行政区划代码省级sql 编辑:程序博客网 时间:2024/05/18 00:46
#include<iostream>
#include<cstdlib>
#include<vector>
#include<ctime>
#include<string>
#include<functional>
#include<algorithm>
using namespace std;
//函数对象适配器bind1st,bind2nd
struct MyPrint :public binary_function<int, int, void>//binary_function<参数类型,参数类型,返回值类型>
{
void operator()(int v1, int v2) const
{
cout << v1 + v2 << endl;
}
};
void test0()
{
vector<int> v;
int i;
for (i = 0;i < 10;i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), bind2nd(MyPrint(), 10));//vector元素,加10后输出
//bind2nd,将参数绑定为函数对象的第二个参数
//bind1st,将参数绑定为函数对象的第一个参数
}
//函数对象适配器not1,not2
struct Myprint
{
void operator()(int a) const
{
cout << a << endl;
}
};
void test1()
{
vector<int> v;
v.push_back(3);v.push_back(5);v.push_back(1);v.push_back(4);v.push_back(6);v.push_back(2);
vector<int>::iterator it = find_if(v.begin(), v.end(), not1(bind2nd(less_equal<int>(), 2)));
cout << *it << endl;
sort(v.begin(), v.end(), not2(greater<int>()));
//not1对一元函数对象取反
//not2对二元函数对象取反
for_each(v.begin(), v.end(), Myprint());
}
//给一个普通函数使用绑定适配器
void fun(int v1, int v2)
{
cout << v1 + v2 << endl;
}
void test2()
{
vector<int> v;
v.push_back(2);v.push_back(1);v.push_back(4);v.push_back(6);v.push_back(3);
for_each(v.begin(), v.end(), bind2nd(ptr_fun(fun), 10));//ptr_fun将普通函数转变为函数对象
}
//指定某个成员函数处理成员数据
class Person
{
private:
string name;
int age;
public:
Person(string name, int age)
{
this->name = name;
this->age = age;
}
void printPerson()
{
cout << this->name << ":" << this->age << endl;
}
};
void test3()
{
vector<Person> v;
v.push_back(Person("liming", 20));v.push_back(Person("zhaohong", 21));
v.push_back(Person("liguan", 19));v.push_back(Person("wangfei", 20));
for_each(v.begin(), v.end(), mem_fun_ref(&Person::printPerson));//如果vector中存储的是对象,则用mem_fun_ref
//如果vector中存储的是指针,则用mem_fun
}
int main()
{
test0();
test1();
test2();
test3();
system("pause");
return 0;
}
#include<cstdlib>
#include<vector>
#include<ctime>
#include<string>
#include<functional>
#include<algorithm>
using namespace std;
//函数对象适配器bind1st,bind2nd
struct MyPrint :public binary_function<int, int, void>//binary_function<参数类型,参数类型,返回值类型>
{
void operator()(int v1, int v2) const
{
cout << v1 + v2 << endl;
}
};
void test0()
{
vector<int> v;
int i;
for (i = 0;i < 10;i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), bind2nd(MyPrint(), 10));//vector元素,加10后输出
//bind2nd,将参数绑定为函数对象的第二个参数
//bind1st,将参数绑定为函数对象的第一个参数
}
//函数对象适配器not1,not2
struct Myprint
{
void operator()(int a) const
{
cout << a << endl;
}
};
void test1()
{
vector<int> v;
v.push_back(3);v.push_back(5);v.push_back(1);v.push_back(4);v.push_back(6);v.push_back(2);
vector<int>::iterator it = find_if(v.begin(), v.end(), not1(bind2nd(less_equal<int>(), 2)));
cout << *it << endl;
sort(v.begin(), v.end(), not2(greater<int>()));
//not1对一元函数对象取反
//not2对二元函数对象取反
for_each(v.begin(), v.end(), Myprint());
}
//给一个普通函数使用绑定适配器
void fun(int v1, int v2)
{
cout << v1 + v2 << endl;
}
void test2()
{
vector<int> v;
v.push_back(2);v.push_back(1);v.push_back(4);v.push_back(6);v.push_back(3);
for_each(v.begin(), v.end(), bind2nd(ptr_fun(fun), 10));//ptr_fun将普通函数转变为函数对象
}
//指定某个成员函数处理成员数据
class Person
{
private:
string name;
int age;
public:
Person(string name, int age)
{
this->name = name;
this->age = age;
}
void printPerson()
{
cout << this->name << ":" << this->age << endl;
}
};
void test3()
{
vector<Person> v;
v.push_back(Person("liming", 20));v.push_back(Person("zhaohong", 21));
v.push_back(Person("liguan", 19));v.push_back(Person("wangfei", 20));
for_each(v.begin(), v.end(), mem_fun_ref(&Person::printPerson));//如果vector中存储的是对象,则用mem_fun_ref
//如果vector中存储的是指针,则用mem_fun
}
int main()
{
test0();
test1();
test2();
test3();
system("pause");
return 0;
}
阅读全文
0 0
- 函数对象及适配器
- 函数对象适配器
- STL-函数对象适配器
- STL-函数对象适配器
- 函数对象的函数适配器
- 函数对象的函数适配器
- 函数对象的函数适配器
- STL函数对象及函数对象适配器
- STL-函数对象及函数对象适配器
- STL函数对象及函数对象适配器
- STL函数对象及函数对象适配器
- STL函数对象及函数对象适配器
- STL函数对象及函数对象适配器
- 函数对象适配器 bind2nd bind1st
- 函数对象 谓词函数 函数适配器
- 函数对象、函数适配器和算法
- unaryPred函数对象,函数适配器(一)
- C++适配器功能实现及函数对象
- 逻辑问题
- 二叉查找树专题总结
- 从github上添加依赖出现无法解析的错误
- 1~9 分为 1:2:3 的三个三位数
- 01-pandas学习引言
- 函数对象适配器
- python爬虫获取11选5彩票历史开奖号码
- 解读 Kotlin/Native 技术预览版
- linux
- C++作业--数组2
- Head First 设计模式学习——装饰者模式
- 关于bootstrap 对<input type ="file">的美化
- 适配器模式的应用
- c++实验5-项目5