目录
可变参数模版
获取参数包值的方式
1.递归方式展开参数包
2.使用数组+逗号表达式展开
emplace_back函数
lambda表达式
C++98中的例子
lambda表达式
语法
lambda表达式和函数比较
包装器
function包装器
bind绑定器
可变参数模版
// Args是一个模板参数包,args是一个函数形参参数包
// 声明一个参数包Args...args,这个参数包中可以包含0到任意个模板参数。
template <class ...Args>
void ShowList(Args... args)
{
}
我们可以发现其实参数包就是将多个模版参数类型和参数都使用Args包装起来了,为了辨别与普通参数的区别,所以三个点是必要的;
获取参数包值的方式
1.递归方式展开参数包
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<functional>
using namespace std;
//结束递归函数
template<class T>
void ShowList(const T& value)//但最后只有一个参数时,调用该函数停止
{
	cout << value << endl;
} 
// Args是一个模板参数包,args是一个函数形参参数包
// 声明一个参数包Args...args,这个参数包中可以包含0到任意个模板参数。
template <class T, class ...Args>
void ShowList(T value, Args... args)
{
	cout << value << " ";
	ShowList(args...);         //递归下一次,参数包中会分理出一个参数value;
}
int main()   
{
	//可以传任意多个参数
	ShowList(1, "564", 5.4); 
	ShowList(1, 2, 5, 8, 4, 65, 6, 5);
	return 0;
}参数包的优势就是参数不是固定的,可以随意的传递多个参数,递归是调用模版函数,每次都会从参数包中分出若干个参数使用;直到最后剩下的个数正好等于ShowList的使用参数个数(一个)时可传递参数时,就要手写一个停止的函数;
按照这样的方式,当然,也可以一次从参数包中分离出多个参数使用,但是最后要确保参数包中的参数要没有剩余;
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<functional>
using namespace std;
//结束递归函数
template<class T,class R>
void ShowList(T& val1,R& val2 )//但最后只有两个参数时(参数包没有剩余时),调用该函数停止 
{
	cout << val1 << " " << val2 << endl;      
	cout << "结束了" << endl; 
} 
// Args是一个模板参数包,args是一个函数形参参数包
// 声明一个参数包Args...args,这个参数包中可以包含0到任意个模板参数。
template <class T, class R,class ...Args>
void ShowList(T value1,R value2 ,Args... args)//可以一次分裂出多个参数使用,但是要确保最后参数包没有多余的参数
{
	cout << value1 << " "<<value2<<" ";
	ShowList(args...);         //递归下一次,参数包中会分理出两个参数value;
}
int main()   
{
	//上面的函数依次使用了两个参数,所以实参必须是2的倍数
	ShowList(1, "564", 5.4,8); 
	ShowList(1, 2, 5, 8, 4, 65, 6, 5);
	return 0;
}
运行结果: ;除了这种方法之外,还可以使用数组结合逗号表达式来一次性使用所有的参数;
;除了这种方法之外,还可以使用数组结合逗号表达式来一次性使用所有的参数;
2.使用数组+逗号表达式展开
template<class T >
void print(T value)    //每次调用函数都会分离出一个参数
{
	cout << value << " ";
}
template<class ...Args>
void ShowList(Args... args)
{
	int arr[] = { (print(args),0)... };     //使用逗号表达式,间接的执行函数;
	return;
}
int main()
{
	ShowList("456", 2, 45, 616 );
	return 0;
}emplace_back函数
template <class... Args>
void emplace_back (Args&&... args);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'));//否则就需要传递list的变量
	mylist.push_back(make_pair(40, 'd'));
	mylist.push_back({ 50, 'e' });//列表初始化构造pair
	for (auto e : mylist)
		cout << e.first << ":" << e.second << endl;
	return 0;
}以上面的代码为例,如果我们尾插节点,通常是需要先构造pair类型的变量,然后再尾插;C++11出来后由于列表初始化的出现,使用{}可以自动调用构造函数初始化,极大地方便了我们;但是除此之外;可变参数包的使用,使得emplace_back可以直接传递pair的参数就可以自动按照顺序调用构造;类似与上面一次使用多个参数的情况;
注意:emplace_back与push_back都只能依次尾插一个节点;
lambda表达式
C++98中的例子
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;
}其中需要注意的就是greater是个仿函数;
这里补充下仿函数的知识:
什么是仿函数?
仿函数实质上是一个重载了括号的模版类;通常适用于比较自定义类型的大小,按照某一特性进行排序的方法;
 上述代码的仿函数可以这样实现;
template<class T>
class _greater//为了避免与std中的冲突
{
public:
	bool operator ()(T a, T b)     
	{
		return a > b;
	}
};
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>());
	for (auto& e : array)
		cout << e << " ";
	return 0;
}为什么要用仿函数,仿函数和普通的比较函数有什么区别?
答:1.灵活性和扩展性
普通比较函数:
 普通比较函数的灵活性相对较低。它们只能执行静态的比较操作,并且不能存储状态或其他信息。
 仿函数:
 仿函数可以存储状态和其他成员变量,这使得它们在某些情况下比普通函数更强大。例如,你可以使用仿函数来维护计数器或其他状态信息:
  class CompareWithCount {
  public:
      CompareWithCount() : count(0) {}
      bool operator()(int a, int b) {
          ++count;
          return a < b;
      }
      int getCount() const { return count; }
  private:
      int count;
  };
在这种情况下,你可以在排序操作后检查比较操作的次数:
  CompareWithCount comp;
  std::sort(arr, arr + size, comp);
  std::cout << "Number of comparisons: " <<comp.getCount() << std::endl;2.性能和优化
普通比较函数:
 普通比较函数通常会有较少的开销,因为它们没有对象的创建和销毁开销,也没有成员变量的存储。
 仿函数:
 仿函数可以在编译时内联,因此在某些情况下,它们可以比普通函数更高效,尤其是当仿函数的 operator() 被内联时。此外,仿函数的成员变量可以在执行时保持状态,这在某些复杂的操作中可能提供性能优势。
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());
}lambda表达式
先展示下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; });
}语法
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;
	//需要使用mutable才能改变拷贝的x;
	auto add_x = [x](int a) mutable { x *= 2; return a + x; };
	cout << add_x(10) << endl;        
	return 0;
}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;
}lambda表达式和函数比较
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;
} 
 
 包装器
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;
}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;//包装成员函数需要多一个参数Plus(隐含的this)
 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;
}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);// 使用举例
#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; //1-2
 cout << func4(1, 2) << endl;//2-1
 return 0;
}注意:为了方便使用,可以以using placeholders::_1的形式使用;


















