目录
7.5 完善转发
8 新的类功能
9 可变参数模板
10 lambda表达式
11 包装器
7.5 完善转发
模板中的 && 万能引用 - void Fun(int &x){ cout << "左值引用" << endl; }
- void Fun(const int &x){ cout << "const 左值引用" << endl; }
- void Fun(int &&x){ cout << "右值引用" << endl; }
- void Fun(const int &&x){ cout << "const 右值引用" << endl; }
- // 模板中的&&不代表右值引用,而是万能引用,其既能接收左值又能接收右值。
- // 模板的万能引用只是提供了能够接收同时接收左值引用和右值引用的能力,
- // 但是引用类型的唯一作用就是限制了接收的类型,后续使用中都退化成了左值,
- // 我们希望能够在传递过程中保持它的左值或者右值的属性, 就需要用我们下面学习的完美转发
- template<typename T>
- void PerfectForward(T&& t)
- {
- Fun(t);
- }
- int main()
- {
- PerfectForward(10); // 右值
- int a;
- PerfectForward(a); // 左值
- PerfectForward(std::move(a)); // 右值
- const int b = 8;
- PerfectForward(b); // const 左值
- PerfectForward(std::move(b)); // const 右值
- return 0;
- }
复制代码 std::forward 完善转发在传参的过程中保存对象原生范例属性 - void Fun(int &x){ cout << "左值引用" << endl; }
- void Fun(const int &x){ cout << "const 左值引用" << endl; }
- void Fun(int &&x){ cout << "右值引用" << endl; }
- void Fun(const int &&x){ cout << "const 右值引用" << endl; }
- // std::forward<T>(t)在传参的过程中保持了t的原生类型属性。
- template<typename T>
- void PerfectForward(T&& t)
- {
- Fun(std::forward<T>(t));
- }
- int main()
- {
- PerfectForward(10); // 右值
- int a;
- PerfectForward(a); // 左值
- PerfectForward(std::move(a)); // 右值
- const int b = 8;
- PerfectForward(b); // const 左值
- PerfectForward(std::move(b)); // const 右值
- return 0;
- }
复制代码 完善转发实际中的利用场景: - template<class T>
- struct ListNode
- {
- ListNode* _next = nullptr;
- ListNode* _prev = nullptr;
- T _data;
- };
- template<class T>
- class List
- {
- typedef ListNode<T> Node;
- public:
- List()
- {
- _head = new Node;
- _head->_next = _head;
- _head->_prev = _head;
- }
- void PushBack(T&& x)
- {
- //Insert(_head, x);
- Insert(_head, std::forward<T>(x));
- }
- void PushFront(T&& x)
- {
- //Insert(_head->_next, x);
- Insert(_head->_next, std::forward<T>(x));
- }
- void Insert(Node* pos, T&& x)
- {
- Node* prev = pos->_prev;
- Node* newnode = new Node;
- newnode->_data = std::forward<T>(x); // 关键位置
- // prev newnode pos
- prev->_next = newnode;
- newnode->_prev = prev;
- newnode->_next = pos;
- pos->_prev = newnode;
- }
- void Insert(Node* pos, const T& x)
- {
- Node* prev = pos->_prev;
- Node* newnode = new Node;
- newnode->_data = x; // 关键位置
- // prev newnode pos
- prev->_next = newnode;
- newnode->_prev = prev;
- newnode->_next = pos;
- pos->_prev = newnode;
- }
- private:
- Node* _head;
- };
- int main()
- {
- List<bit::string> lt;
- lt.PushBack("1111");
- lt.PushFront("2222");
- return 0;
- }
复制代码 8 新的类功能
默认成员函数 原来 C++ 类中,有 6 个默认成员函数: 1. 构造函数 2. 析构函数 3. 拷贝构造函数 4. 拷贝赋值重载 5. 取所在重载 6. const 取所在重载 最后紧张的是前 4 个,后两个用处不大。默认成员函数就是我们不写编译器会天生一个默认的。 C++11 新增了两个:移动构造函数和移动赋值运算符重载。 针对移动构造函数和移动赋值运算符重载有一些需要注意的点如下: 假如你没有本身实现移动构造函数,且没有实现析构函数 、拷贝构造、拷贝赋值重载中的任 意一个。那么编译器会主动天生一个默认移动构造。默认天生的移动构造函数,对于内置类 型成员会执行逐成员按字节拷贝,自界说范例成员,则需要看这个成员是否实现移动构造, 假如实现了就调用移动构造,没有实现就调用拷贝构造。 假如你没有本身实现移动赋值重载函数,且没有实现析构函数 、拷贝构造、拷贝赋值重载中 的任意一个,那么编译器会主动天生一个默认移动赋值。默认天生的移动构造函数,对于内 置范例成员会执行逐成员按字节拷贝,自界说范例成员,则需要看这个成员是否实现移动赋 值,假如实现了就调用移动赋值,没有实现就调用拷贝赋值。 ( 默认移动赋值跟上面移动构造 完全类似 ) 假如你提供了移动构造或者移动赋值,编译器不会主动提供拷贝构造和拷贝赋值。 - // 以下代码在vs2013中不能体现,在vs2019下才能演示体现上面的特性。
- class Person
- {
- public:
- Person(const char* name = "", int age = 0)
- :_name(name)
- , _age(age)
- {}
- /*Person(const Person& p)
- :_name(p._name)
- ,_age(p._age)
- {}*/
- /*Person& operator=(const Person& p)
- {
- if(this != &p)
- {
- _name = p._name;
- _age = p._age;
- }
- return *this;
- }*/
- /*~Person()
- {}*/
- private:
- bit::string _name;
- int _age;
- };
- int main()
- {
- Person s1;
- Person s2 = s1;
- Person s3 = std::move(s1);
- Person s4;
- s4 = std::move(s2);
- return 0;
- }
复制代码 类成员变量初始化 C++11 允许在类界说时给成员变量初始缺省值,默认天生构造函数会利用这些缺省值初始化,这 个我们在雷和对象默认就讲了,这里就不再细讲了。 逼迫天生默认函数的关键字 default: C++11 可以让你更好的控制要利用的默认函数。假设你要利用某个默认的函数,但是因为一些原 因这个函数没有默认天生。比如:我们提供了拷贝构造,就不会天生移动构造了,那么我们可以 利用 default 关键字显示指定移动构造天生。 - class Person
- {
- public:
- Person(const char* name = "", int age = 0)
- :_name(name)
- , _age(age)
- {}
- Person(const Person& p)
- :_name(p._name)
- ,_age(p._age)
- {}
- Person(Person&& p) = default;
- private:
- bit::string _name;
- int _age;
- };
- int main()
- {
- Person s1;
- Person s2 = s1;
- Person s3 = std::move(s1);
- return 0;
- }
复制代码 禁止天生默认函数的关键字 delete: 假如能想要限制某些默认函数的天生,在 C++98 中,是该函数设置成 private ,并且只声明补丁 已,这样只要其他人想要调用就会报错。在 C++11 中更简朴,只需在该函数声明加上 =delete 即 可,该语法指示编译器不天生对应函数的默认版本,称 =delete 修饰的函数为删除函数。 - class Person
- {
- public:
- Person(const char* name = "", int age = 0)
- :_name(name)
- , _age(age)
- {}
- Person(const Person& p) = delete;
- private:
- bit::string _name;
- int _age;
- };
- int main()
- {
- Person s1;
- Person s2 = s1;
- Person s3 = std::move(s1);
- return 0;
- }
复制代码 9 可变参数模板
C++11 的新特性可变参数模板可以或许让您创建可以接受可变参数的函数模板和类模板,相比 C++98/03 ,类模版和函数模版中只能含固定命量的模版参数,可变模版参数无疑是一个巨大的改 进。然而由于可变模版参数比较抽象,利用起来需要一定的技巧,所以这块照旧比较艰涩的。现 阶段呢,我们把握一些基础的可变参数模板特性就够我们用了,所以这里我们点到为止,以后大 家假如有需要,再可以深入学习。 下面就是一个根本可变参数的函数模板 - // Args是一个模板参数包,args是一个函数形参参数包
- // 声明一个参数包Args...args,这个参数包中可以包含0到任意个模板参数。
- template <class ...Args>
- void ShowList(Args... args)
- {}
复制代码 上面的参数 args 前面有省略号,所以它就是一个可变模版参数,我们把带省略号的参数称为 “ 参数 包 ” ,它里面包含了 0 到 N ( N>=0 )个模版参数。我们无法直接获取参数包 args 中的每个参数的, 只能通过展开参数包的方式来获取参数包中的每个参数,这是利用可变模版参数的一个主要特 点,也是最大的难点,即如何展开可变模版参数。由于语法不支持利用 args 这样方式获取可变 参数,所以我们的用一些奇招来一一获取参数包的值。 递归函数方式展开参数包 - // 递归终止函数
- template <class T>
- void ShowList(const T& t)
- {
- cout << t << endl;
- }
- // 展开函数
- template <class T, class ...Args>
- void ShowList(T value, Args... args)
- {
- cout << value <<" ";
- ShowList(args...);
- }
- int main()
- {
- ShowList(1);
- ShowList(1, 'A');
- ShowList(1, 'A', std::string("sort"));
- return 0;
- }
复制代码 STL 容器中的 empalce 相干接口函数: http://www.cplusplus.com/reference/vector/vector/emplace_back/ http://www.cplusplus.com/reference/list/list/emplace_back/ 起首我们看到的 emplace 系列的接口,支持模板的可变参数,并且万能引用。那么相对 insert 和 emplace 系列接口的上风到底在哪里呢? - int main()
- {
- std::list< std::pair<int, char> > mylist;
- // emplace_back支持可变参数,拿到构建pair对象的参数后自己去创建对象
- // 那么在这里我们可以看到除了用法上,和push_back没什么太大的区别
- mylist.emplace_back(10, 'a');
- mylist.emplace_back(20, 'b');
- mylist.emplace_back(make_pair(30, 'c'));
- mylist.push_back(make_pair(40, 'd'));
- mylist.push_back({ 50, 'e' });
- for (auto e : mylist)
- cout << e.first << ":" << e.second << endl;
- return 0;
- }
复制代码 - int main()
- {
- // 下面我们试一下带有拷贝构造和移动构造的bit::string,再试试呢
- // 我们会发现其实差别也不到,emplace_back是直接构造了,push_back
- // 是先构造,再移动构造,其实也还好。
- std::list< std::pair<int, bit::string> > mylist;
- mylist.emplace_back(10, "sort");
- mylist.emplace_back(make_pair(20, "sort"));
- mylist.push_back(make_pair(30, "sort"));
- mylist.push_back({ 40, "sort"});
- return 0;
- }
复制代码 10 lambda表达式
10.1 C++98 中的一个例子 在 C++98 中,假如想要对一个数据集合中的元素进行排序,可以利用 std::sort 方法。 - #include <algorithm>
- #include <functional>
- int main()
- {
- int array[] = {4,1,8,5,3,7,0,9,2,6};
- // 默认按照小于比较,排出来结果是升序
- std::sort(array, array+sizeof(array)/sizeof(array[0]));
- // 如果需要降序,需要改变元素的比较规则
- std::sort(array, array + sizeof(array) / sizeof(array[0]), greater<int>());
- return 0;
- }
复制代码 假如待排序元素为自界说范例,需要用户界说排序时的比较规则: - struct Goods
- {
- string _name; // 名字
- double _price; // 价格
- int _evaluate; // 评价
- Goods(const char* str, double price, int evaluate)
- :_name(str)
- , _price(price)
- , _evaluate(evaluate)
- {}
- };
- struct ComparePriceLess
- {
- bool operator()(const Goods& gl, const Goods& gr)
- {
- return gl._price < gr._price;
- }
- };
- struct ComparePriceGreater
- {
- bool operator()(const Goods& gl, const Goods& gr)
- {
- return gl._price > gr._price;
- }
- };
- int main()
- {
- vector<Goods> v = { { "苹果", 2.1, 5 }, { "香蕉", 3, 4 }, { "橙子", 2.2,
- 3 }, { "菠萝", 1.5, 4 } };
- sort(v.begin(), v.end(), ComparePriceLess());
- sort(v.begin(), v.end(), ComparePriceGreater());
- }
复制代码 随着 C++ 语法的发展, 人们开始觉得上面的写法太复杂了,每次为了实现一个 algorithm 算法, 都要重新去写一个类,假如每次比较的逻辑不一样,还要去实现多个类,特殊是相同类的定名, 这些都给编程者带来了极大的未便 。因此,在 C++11 语法中出现了 Lambda 表达式。 10.2 lambda 表达式 - int main()
- {
- vector<Goods> v = { { "苹果", 2.1, 5 }, { "香蕉", 3, 4 }, { "橙子", 2.2,
- 3 }, { "菠萝", 1.5, 4 } };
- sort(v.begin(), v.end(), [](const Goods& g1, const Goods& g2){
- return g1._price < g2._price; });
- sort(v.begin(), v.end(), [](const Goods& g1, const Goods& g2){
- return g1._price > g2._price; });
- sort(v.begin(), v.end(), [](const Goods& g1, const Goods& g2){
- return g1._evaluate < g2._evaluate; });
- sort(v.begin(), v.end(), [](const Goods& g1, const Goods& g2){
- return g1._evaluate > g2._evaluate; });
- }
复制代码 上述代码就是利用 C++11 中的 lambda 表达式来解决,可以看出 lambda 表达式实际是一个匿名函 数。 10.3 lambda 表达式语法 lambda 表达式誊写格式: [capture-list] (parameters) mutable -> return-type { statement } 1. lambda 表达式各部分说明 [capture-list] : 捕捉列表 ,该列表总是出如今 lambda 函数的开始位置, 编译器根据 [] 来 判定接下来的代码是否为 lambda 函数 , 捕捉列表可以或许捕捉上下文中的变量供 lambda 函数利用 。 (parameters) :参数列表。与 普通函数的参数列表划一 ,假如不需要参数传递,则可以 连同 () 一起省略 mutable :默认情况下, lambda 函数总是一个 const 函数, mutable 可以取消其常量 性。利用该修饰符时,参数列表不可省略 ( 纵然参数为空 ) 。 ->returntype :返回值范例 。用 追踪返回范例情势声明函数的返回值范例 ,没有返回 值时此部分可省略。 返回值范例明确情况下,也可省略,由编译器对返回范例进行推 导 。 {statement} :函数体 。在该函数体内,除了可以利用其参数外,还可以利用所有捕获 到的变量。 注意: 在 lambda 函数界说中, 参数列表和返回值范例都是可选部分,而捕捉列表和函数体可以为 空 。因此 C++11 中 最简朴的 lambda 函数为: []{} ; 该 lambda 函数不能做任何事情。 - int main()
- {
- // 最简单的lambda表达式, 该lambda表达式没有任何意义
- []{};
-
- // 省略参数列表和返回值类型,返回值类型由编译器推导为int
- int a = 3, b = 4;
- [=]{return a + 3; };
-
- // 省略了返回值类型,无返回值类型
- auto fun1 = [&](int c){b = a + c; };
- fun1(10)
- cout<<a<<" "<<b<<endl;
-
- // 各部分都很完善的lambda函数
- auto fun2 = [=, &b](int c)->int{return b += a+ c; };
- cout<<fun2(10)<<endl;
-
- // 复制捕捉x
- int x = 10;
- auto add_x = [x](int a) mutable { x *= 2; return a + x; };
- cout << add_x(10) << endl;
- return 0;
- }
复制代码 通过上述例子可以看出, lambda 表达式实际上可以理解为无名函数,该函数无法直接调 用,假如想要直接调用,可借助 auto 将其赋值给一个变量。 2. 捕获列表说明 捕捉列表描述了上下文中那些数据可以被 lambda 利用 ,以及 利用的方式传值照旧传引用 。 [var] :表示值传递方式捕捉变量 var [=] :表示值传递方式捕获所有父作用域中的变量 ( 包罗 this) [&var] :表示引用传递捕捉变量 var [&] :表示引用传递捕捉所有父作用域中的变量 ( 包罗 this) [this] :表示值传递方式捕捉当前的 this 指针 注意: a. 父作用域指包含 lambda 函数的语句块 b. 语法上捕捉列表可由多个捕捉项组成,并以逗号分割 。 比如: [=, &a, &b] :以引用传递的方式捕捉变量 a 和 b ,值传递方式捕捉其他所有变量 [& , a, this] :值传递方式捕捉变量 a 和 this ,引用方式捕捉其他变量 c. 捕捉列表不允许变量重复传递,否则就会导致编译错误 。 比如: [=, a] : = 已经以值传递方式捕捉了所有变量,捕捉 a 重复 d. 在块作用域以外的 lambda 函数捕捉列表必须为空 。 e. 在块作用域中的 lambda 函数仅能捕捉父作用域中局部变量,捕捉任何非此作用域或者 非局部变量都 会导致编译报错。 f. lambda 表达式之间不能相互赋值 ,纵然看起来范例相同 - void (*PF)();
- int main()
- {
- auto f1 = []{cout << "hello world" << endl; };
- auto f2 = []{cout << "hello world" << endl; };
- // 此处先不解释原因,等lambda表达式底层实现原理看完后,大家就清楚了
- //f1 = f2; // 编译失败--->提示找不到operator=()
- // 允许使用一个lambda表达式拷贝构造一个新的副本
- auto f3(f2);
- f3();
- // 可以将lambda表达式赋值给相同类型的函数指针
- PF = f2;
- PF();
- return 0;
- }
- class Rate
- {
- public:
- Rate(double rate): _rate(rate)
- {}
- double operator()(double money, int year)
- { return money * _rate * year;}
- private:
- double _rate;
- };
- int main()
- {
- // 函数对象
- double rate = 0.49;
- Rate r1(rate);
- r1(10000, 2);
- // lamber
- auto r2 = [=](double monty, int year)->double{return monty*rate*year;
- };
- r2(10000, 2);
- return 0;
- 比
复制代码 10.4 函数对象与 lambda 表达式 函数对象,又称为仿函数,即可以想函数一样利用的对象,就是在类中重载了 operator() 运算符的 类对象。 - class Rate
- {
- public:
- Rate(double rate): _rate(rate)
- {}
- double operator()(double money, int year)
- { return money * _rate * year;}
- private:
- double _rate;
- };
- int main()
- {
- // 函数对象
- double rate = 0.49;
- Rate r1(rate);
- r1(10000, 2);
- // lamber
- auto r2 = [=](double monty, int year)->double{return monty*rate*year;
- };
- r2(10000, 2);
- return 0;
- }
复制代码 从利用方式上来看,函数对象与 lambda 表达式完全一样。 函数对象将 rate 作为其成员变量,在界说对象时给出初始值即可, lambda 表达式通过捕获列表可 以直接将该变量捕获到。
实际在底层编译器对于 lambda 表达式的处理方式,完全就是按照函数对象的方式处理的,即:如 果界说了一个 lambda 表达式,编译器会主动天生一个类,在该类中重载了 operator() 。
11 包装器
function 包装器 function 包装器 也叫作适配器。 C++ 中的 function 本质是一个类模板,也是一个包装器。 那么我们来看看,我们为什么需要 function 呢? - ret = func(x);
- // 上面func可能是什么呢?那么func可能是函数名?函数指针?函数对象(仿函数对象)?也有可能
- 是lamber表达式对象?所以这些都是可调用的类型!如此丰富的类型,可能会导致模板的效率低下!
- 为什么呢?我们继续往下看
- template<class F, class T>
- T useF(F f, T x)
- {
- static int count = 0;
- cout << "count:" << ++count << endl;
- cout << "count:" << &count << endl;
- return f(x);
- }
- double f(double i)
- {
- return i / 2;
- }
- struct Functor
- {
- double operator()(double d)
- {
- return d / 3;
- }
- };
- int main()
- {
- // 函数名
- cout << useF(f, 11.11) << endl;
- // 函数对象
- cout << useF(Functor(), 11.11) << endl;
- // lamber表达式
- cout << useF([](double d)->double{ return d/4; }, 11.11) << endl;
- return 0;
- }
复制代码 通过上面的步伐验证,我们会发现 useF 函数模板实例化了三份。 包装器可以很好的解决上面的问题 - std::function在头文件<functional>
- // 类模板原型如下
- template <class T> function; // undefined
- template <class Ret, class... Args>
- class function<Ret(Args...)>;
- 模板参数说明:
- Ret: 被调用函数的返回类型
- Args…:被调用函数的形参
- // 使用方法如下:
- #include <functional>
- int f(int a, int b)
- {
- return a + b;
- }
- struct Functor
- {
- public:
- int operator() (int a, int b)
- {
- return a + b;
- }
- };
- class Plus
- {
- public:
- static int plusi(int a, int b)
- {
- return a + b;
- }
- double plusd(double a, double b)
- {
- return a + b;
- }
- };
- int main()
- {
- // 函数名(函数指针)
- std::function<int(int, int)> func1 = f;
- cout << func1(1, 2) << endl;
- // 函数对象
- std::function<int(int, int)> func2 = Functor();
- cout << func2(1, 2) << endl;
- // lamber表达式
- std::function<int(int, int)> func3 = [](const int a, const int b)
- {return a + b; };
- cout << func3(1, 2) << endl;
-
- // 类的成员函数
- std::function<int(int, int)> func4 = &Plus::plusi;
- cout << func4(1, 2) << endl;
- std::function<double(Plus, double, double)> func5 = &Plus::plusd;
- cout << func5(Plus(), 1.1, 2.2) << endl;
- return 0;
- }
复制代码 有了包装器,如何解决模板的效率低下,实例化多份的问题呢? - #include <functional>
- template<class F, class T>
- T useF(F f, T x)
- {
- static int count = 0;
- cout << "count:" << ++count << endl;
- cout << "count:" << &count << endl;
- return f(x);
- }
- double f(double i)
- {
- return i / 2;
- }
- struct Functor
- {
- double operator()(double d)
- {
- return d / 3;
- }
- };
- int main()
- {
- // 函数名
- std::function<double(double)> func1 = f;
- cout << useF(func1, 11.11) << endl;
- // 函数对象
- std::function<double(double)> func2 = Functor();
- cout << useF(func2, 11.11) << endl;
- // lamber表达式
- std::function<double(double)> func3 = [](double d)->double{ return d /
- 4; };
- cout << useF(func3, 11.11) << endl;
- return 0;
- }
复制代码 包装器的其他一些场景: https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/submissions/ - class Solution {
- public:
- int evalRPN(vector<string>& tokens) {
- stack<int> st;
- for(auto& str : tokens)
- {
- if(str == "+" || str == "-" || str == "*" || str == "/")
- {
- int right = st.top();
- st.pop();
- int left = st.top();
- st.pop();
- switch(str[0])
- {
- case '+':
- st.push(left+right);
- break;
- case '-':
- st.push(left-right);
- break;
- case '*':
- st.push(left*right);
- break;
- case '/':
- st.push(left/right);
- break;
- }
- }
- else
- {
- // 1、atoi itoa
- // 2、sprintf scanf
- // 3、stoi to_string C++11
- st.push(stoi(str));
- }
- }
- return st.top();
- }
- };
- // 使用包装器以后的玩法
- class Solution {
- public:
- int evalRPN(vector<string>& tokens) {
- stack<int> st;
- map<string, function<int(int, int)>> opFuncMap =
- {
- { "+", [](int i, int j){return i + j; } },
- { "-", [](int i, int j){return i - j; } },
- { "*", [](int i, int j){return i * j; } },
- { "/", [](int i, int j){return i / j; } }
- };
- for(auto& str : tokens)
- {
- if(opFuncMap.find(str) != opFuncMap.end())
- {
- int right = st.top();
- st.pop();
- int left = st.top();
- st.pop();
- st.push(opFuncMap[str](left, right));
- }
- else
- {
- // 1、atoi itoa
- // 2、sprintf scanf
- // 3、stoi to_string C++11
- st.push(stoi(str));
- }
- }
- return st.top();
- }
- };
复制代码 bind std::bind 函数界说在头文件中, 是一个函数模板,它就像一个函数包装器 ( 适配器 ) , 接受一个可 调用对象( callable object ),天生一个新的可调用对象来 “ 顺应 ” 原对象的参数列表 。一样平常而 言,我们用它可以把一个本来接收 N 个参数的函数 fn ,通过绑定一些参数,返回一个接收 M 个( M 可以大于 N ,但这么做没什么意义)参数的新函数。同时,利用 std::bind 函数还可以实现参数顺 序调整等操作。 - // 原型如下:
- template <class Fn, class... Args>
- /* unspecified */ bind (Fn&& fn, Args&&... args);
- // with return type (2)
- template <class Ret, class Fn, class... Args>
- /* unspecified */ bind (Fn&& fn, Args&&... args);
复制代码 可以将 bind 函数看作是一个通用的函数适配器,它接受一个可调用对象,天生一个新的可调用对 象来 “ 顺应 ” 原对象的参数列表。 调用 bind 的一样平常情势: auto newCallable = bind(callable,arg_list); 此中, newCallable 本身是一个可调用对象, arg_list 是一个逗号分隔的参数列表,对应给定的 callable 的参数。 当我们调用 newCallable 时, newCallable 会调用 callable, 并传给它 arg_list 中 的参数 。 arg_list 中的参数大概包含形如 _n 的名字,此中 n 是一个整数,这些参数是 “ 占位符 ” ,表示 newCallable 的参数,它们占据了传递给 newCallable 的参数的 “ 位置 ” 。数值 n 表示天生的可调用对 象中参数的位置: _1 为 newCallable 的第一个参数, _2 为第二个参数,以此类推。 - // 使用举例
- #include <functional>
- int Plus(int a, int b)
- {
- return a + b;
- }
- class Sub
- {
- public:
- int sub(int a, int b)
- {
- return a - b;
- }
- };
- int main()
- {
- //表示绑定函数plus 参数分别由调用 func1 的第一,二个参数指定
- std::function<int(int, int)> func1 = std::bind(Plus, placeholders::_1,
- placeholders::_2);
- //auto func1 = std::bind(Plus, placeholders::_1, placeholders::_2);
- //func2的类型为 function<void(int, int, int)> 与func1类型一样
- //表示绑定函数 plus 的第一,二为: 1, 2
- auto func2 = std::bind(Plus, 1, 2);
- cout << func1(1, 2) << endl;
- cout << func2() << endl;
- Sub s;
- // 绑定成员函数
- std::function<int(int, int)> func3 = std::bind(&Sub::sub, s,
- placeholders::_1, placeholders::_2);
- // 参数调换顺序
- std::function<int(int, int)> func4 = std::bind(&Sub::sub, s,
- placeholders::_2, placeholders::_1);
- cout << func3(1, 2) << endl;
- cout << func4(1, 2) << endl;
- return 0;
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |