右值引用,完美转发,万能引用 用法
template <typename T>
struct __list_node
{
__list_node(const T& val = T())
:_prev(nullptr)
, _next(nullptr)
, _val(val)
{}
__list_node(T&& val)
:_prev(nullptr)
, _next(nullptr)
, _val(forward<T>(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++;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
lambda表达式 用法
#include
#include
#include
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
包装器(类模板function) 用法
#include
#include
#include
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()
{
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
包装器(函数模板bind) 用法
#include
#include
#include
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59