当前位置:网站首页>Swap, move, forward, exchange of utility component learning
Swap, move, forward, exchange of utility component learning
2022-07-25 23:31:00 【I washed my feet in Bangong Lake】
utility Is a practical component library , It includes some very useful functions , such as swap,move,forwardexchange etc. , This article will learn the basic use of these functions .
| Swap the values of two objects ( Function templates ) | |
(C++14) | Replace the argument with a new value , And return its previous value ( Function templates ) |
(C++11) | Forward a function argument ( Function templates ) |
(C++11) | Get the right value reference ( Function templates ) |
(C++11) | If the move constructor does not throw, then get the right value reference ( Function templates ) |
(C++17) | Get the actual parameters const quote ( Function templates ) |
(C++11) | Get the reference of its parameter , Used in the context of non evaluation ( Function templates ) |
1.swap Swap the values of two objects , Example :
#include <iostream>
#include <string>
#include <utility>
using namespace std;
class Yak {
public:
Yak(string name = "", int h = 0, int w = 0):
m_name(name),
m_height(h),
m_weight(w)
{
cout << "constructe============== " << this << " name= " << name << " height= " << h << " weight= " << w << endl;
}
~Yak(){ cout << "destructe~~~~~~~~~~~~~~~ " << this << " name= " << this->m_name << " height= " << m_height << " weight= " << m_weight << endl;}
string m_name{""};
int m_height{0};
int m_weight{0};
};
int main()
{
Yak yak1;
yak1.m_name = "yak1";
yak1.m_height = 10001;
yak1.m_weight = 201;
Yak yak2;
yak2.m_name = "yak2";
yak2.m_height = 10002;
yak2.m_weight = 202;
cout << "Yak1.name= " << yak1.m_name << " Yak2.name= " << yak2.m_name << " yak1 = " << &yak1 << " yak2= " << &yak2 << endl;
cout << "============swap=============" << endl;
std::swap(yak1, yak2);
cout << "Yak1.name= " << yak1.m_name << " Yak2.name= " << yak2.m_name << " yak1 = " << &yak1 << " yak2= " << &yak2 << endl;
cout << "Hello World!" << endl;
return 0;
}
Running results :

From the running results : There is an additional destructor in the exchange , Have you produced a temporary object
The implementation in the original code is T c(std::move(a)); a=std::move(b); b=std::move(c); From this original code, we can see that a temporary object will be generated during the exchange c.
2.move be used for instructions object t Sure “ Be moved ”, That is, from t Efficient resource transfer to another object . especially , std::move Generate parameters that identify them t Of Dead value expression . It is exactly equivalent to a reference to an R-value type static_cast .
Example :
#include <iostream>
#include <string>
#include <vector>
#include <utility>
using namespace std;
int main()
{
string str = "Yak is mao niu";
vector<std::string> vecStr;
// Use push_back(const T&) heavy load ,
// It means that we will bring replication str Cost of
vecStr.push_back(str);
std::cout << "After copy, str is \"" << str << "\"\n";
// Use the right value reference push_back(T&&) heavy load ,
// Indicates that the string is not copied ; It is
// str The content of is moved into vector
// The cost is relatively low , But it also means str Now it may be empty .
vecStr.push_back(std::move(str));
std::cout << "After move, str is \"" << str << "\"\n";
std::cout << "The contents of the vector are \"" << vecStr[0]
<< "\", \"" << vecStr[1] << "\"\n";
cout << "Hello World!" << endl;
return 0;
}
From the above results ,move Value after original str The content is emptied .
Here is a class to demonstrate its value :
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
using namespace std;
class Yak {
public:
Yak(string name = "", int h = 0, int w = 0):
m_name(name),
m_height(h),
m_weight(w)
{
cout << "constructe============== " << this << " name= " << name << " height= " << h << " weight= " << w << endl;
}
~Yak(){ cout << "destructe~~~~~~~~~~~~~~~ " << this << " name= " << this->m_name << " height= " << m_height << " weight= " << m_weight << endl;}
string m_name{""};
int m_height{0};
int m_weight{0};
};
int main()
{
Yak yak1;
yak1.m_name = "yak1";
yak1.m_height = 10001;
yak1.m_weight = 201;
Yak yak2;
yak2.m_name = "yak2";
yak2.m_height = 10002;
yak2.m_weight = 202;
vector<Yak> vec;
vec.reserve(16); // Prevent reallocation when adding data
vec.push_back(yak1);
cout << "Yak2.name= " << yak2.m_name << " Yak2.height= " << yak2.m_height << " yak2.weight = " << yak2.m_weight << endl;
vec.push_back(std::move(yak2));
cout << "Yak2.name= " << yak2.m_name << " Yak2.height= " << yak2.m_height << " yak2.weight = " << yak2.m_weight << endl;
cout << "Hello World!" << endl;
return 0;
}Running results :

Why? move after Yak2 The value of the object has not been cleared ???
Reference resources :
边栏推荐
- Kotlin 常用知识点汇总
- [QNX hypervisor 2.2 user manual]9.7 generate
- 762. 二进制表示中质数个计算置位
- Constructors and prototypes
- Tencent map API request source is not authorized, this request source domain name
- Summary of common PHP functions
- PHP JSON variable array problem
- Serialize operator
- POI特效 市场调研
- Serialize common default values and column parameters
猜你喜欢

@Import

Npm+ module loading mechanism

Query commodity cases (operate data with array addition method) / key points

Tencent map API request source is not authorized, this request source domain name

Anti shake and throttling

ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用

How does PHP remove an element from an array based on the key value

Take away applet with main version of traffic / repair to add main access function of traffic

Docker installation redis-5.0.12 (remote access)

Release of v6.5.1/2/3 series of versions of Xingyun housekeeper: the ability of database OpenAPI continues to be strengthened
随机推荐
[code case] blog page design (with complete source code)
Node Foundation
Very simple vsplayaudio online music player plug-in
Learning exploration-3d rotation card
WordPress controls the minimum and maximum number of words of article comments
numeric学习之iota,accumulate
Several commonly used traversal methods
From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?
Serialize operator
WordPress function encyclopedia, you can do the theme after learning it
[QNX Hypervisor 2.2用户手册]9.8 load
PHP JSON variable array problem
[QNX Hypervisor 2.2用户手册]9.6 gdb
XxE & XML external entity injection utilization and bypass
Classes and objects (2) (6 default member functions)
Call Gaode map -- address is converted into longitude and latitude
关于优先队列
类和对象(3)
How does PHP remove an element from an array based on the key value
Source code of YY music wechat applet imitating Netease cloud music