右值引用, 完美转发, 万能引用, lambda表达式, 包装器 用法

右值引用,完美转发,万能引用 用法

//链表节点
template <typename T>
struct __list_node
{
	//1. 
	__list_node(const T& val = T())
		:_prev(nullptr)
		, _next(nullptr)
		, _val(val)
	{}
	__list_node(T&& val)
		:_prev(nullptr)
		, _next(nullptr)
		, _val(forward<T>(val))
	{}

	//2. 
	/*
	template<typename Ty>
	__list_node(Ty&& val)
		:_prev(nullptr)
		, _next(nullptr)
		, _val(forward<Ty>(val))
	{}
	*/
	
	/*成员变量*/
	__list_node<T>* _prev;
	__list_node<T>* _next;
	T _val;
};

//添加相关
void push_back(const T& x)
{
	Node* newnode = new Node(x);
	Node* tail = _head->_prev;
	tail->_next = newnode;
	newnode->_prev = tail;
	newnode->_next = _head;
	_head->_prev = newnode;
	_size++;
}
//右值引用+完美转发
void push_back(T&& x)
{
	Node* newnode = new Node(forward<T>(x));
	Node* tail = _head->_prev;
	tail->_next = newnode;
	newnode->_prev = tail;
	newnode->_next = _head;
	_head->_prev = newnode;
	_size++;
}

lambda表达式 用法

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

class Goods
{
public:
	Goods(string name, double price, int servicelife, int qualityindex)
		:_name(name)
		,_price(price)
		,_servicelife(servicelife)
		,_qualityindex(qualityindex)
	{}

	void Show()
	{
		cout << "物品名称:" << _name << " ";
		cout << "价格:" << _price << " ";
		cout << "使用期限:" << _servicelife << " ";
		cout << "质量指数:" << _qualityindex << " " << endl;
	}
	string _name;
	double _price;
	int _servicelife;
	int _qualityindex;
};

void Print(Goods* ptr)
{
	for (int i = 0; i < 5; i++)
	{
		(*(ptr + i)).Show();
	}
	cout << endl;
}

int main()
{
	Goods Array[5] = { Goods("Book", 59.9, 100, 10),
		Goods("Computer", 5535.3, 10, 9),
		Goods("Pen", 2.5, 2, 5),
		Goods("Phone", 4500.5, 20, 8),
		Goods("Shoe", 200.5, 1, 7) };

	sort(Array, Array + 5, [](const Goods& thing1, const Goods& thing2) -> bool {return thing1._name < thing2._name; });
	Print(Array);

	sort(Array, Array + 5, [](const Goods& thing1, const Goods& thing2) {return thing1._price < thing2._price; });
	Print(Array);

	sort(Array, Array + 5, [](const Goods& thing1, const Goods& thing2) {return thing1._servicelife < thing2._servicelife; });
	Print(Array);

	sort(Array, Array + 5, [](const Goods& thing1, const Goods& thing2) {return thing1._qualityindex < thing2._qualityindex; });
	Print(Array);

	return 0;
}

包装器(类模板function) 用法

#include <iostream>
#include <functional>
#include <vector>
using namespace std;

//五花八门的仿函数(可调用对象之一)
class FUNC1
{
public:
	int operator()(int x)
	{
		cout << "这是一个仿函数对象: 你传入的是" << x;
		if (x % 2 == 0)
		{
			cout << " , 这是一个偶数, 它的下一个奇数是";
		}
		else
		{
			cout << " , 这是一个奇数, 它的下一个偶数是";
		}
		return x + 1;
	}
};
class FUNC2
{
public:
	int operator()(int x)
	{
		cout << "这是一个仿函数对象: 你传入的是" << x;
		cout << ", 我将其除上10,为:";
		return x / 10;
	}
};

//五花八门的普通函数(可调用对象之一)
int OrdinaryFunc1(int x)
{
	cout << "普通函数:";
	cout << "你传入的是" << x << ", 我将其除2,为:";
	return x / 2;
}
int OrdinaryFunc2(int x)
{
	cout << "普通函数:";
	cout << "你传入的是" << x << ", 我将其除5,为:";
	return x / 5;
}

int main()
{
	//五花八门的lambda表达式(可调用对象之一)
	const char* ptr = "lambda表达式";
	int i = 1;
	auto func1 = [&, ptr](int x) {
		cout << ptr << (i++) << ": ";
		cout << "你传入的是" << x << ", 我将其加上10,为:";
		return x + 10;
	};
	auto func2 = [&](int x) {
		cout << ptr << (i++) << ": ";
		cout << "你传入的是" << x << ", 我将其乘上10,为:";
		return x * 10;
	};
	auto func3 = [&](int x) {
		cout << ptr << (i++) << ": ";
		cout << "你传入的是" << x << ", 我将其减去10,为:";
		return x - 10;
	};

	//统一的方式
	vector<function<int(int)>> MyFuncs({ func1, func2, func3, FUNC1(), FUNC2() ,func1, func2, FUNC2(), OrdinaryFunc1,OrdinaryFunc2 });
	int number = 2;
	for (auto& everyfunc : MyFuncs)
	{
		cout << everyfunc(number) << endl;
		number *=10;
	}
	return 0;
}

包装器(函数模板bind) 用法

#include <iostream>
#include <functional>
#include <vector>
using namespace std;

//可调用对象:全局普通函数
void InfoInput(int age, const string& sex, const string& name)
{
	cout << "姓名:" << name << endl;
	cout << "性别:" << sex << endl;
	cout << "年龄:" << age << endl << endl;
}

//可调用对象:类的普通函数
class Person
{
public:
	void ClassInfoInput(const string& sex, int age, const string& name)
	{
		_name = name;
		_sex = sex;
		_age = age;
	}
	void ShowInfo()
	{
		cout << "姓名:" << _name << endl;
		cout << "性别:" << _sex << endl;
		cout << "年龄:" << _age << endl << endl;
	}
private:
	string _name;
	string _sex;
	int _age;
};

int main()
{
	auto BoyNewInfoInput = bind(InfoInput, placeholders::_2, "男", placeholders::_1);
	auto GirlNewInfoInput = bind(InfoInput, placeholders::_2, "女", placeholders::_1);
	BoyNewInfoInput("沈", 20);
	BoyNewInfoInput("徐", 35);
	GirlNewInfoInput("叶", 19);
	GirlNewInfoInput("黄", 25);

	Person p1, p2;
	auto BoyClassNewInfoInput = bind(&Person::ClassInfoInput, &p1, "男", placeholders::_2, placeholders::_1);
	auto GirlClassNewInfoInput = bind(&Person::ClassInfoInput, &p2, "女", placeholders::_2, placeholders::_1);
	BoyClassNewInfoInput("Mike", 22);
	GirlClassNewInfoInput("Army", 18);
	p1.ShowInfo();
	p2.ShowInfo();

	auto obj = [](int x, int y) {return x < y; };
	auto newobj = bind(obj, placeholders::_2, placeholders::_1);

	cout << obj(1, 2) << endl;
	cout << newobj(1, 2) << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

絕知此事要躬行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值