当前位置:网站首页>Custom swap function
Custom swap function
2022-07-06 22:38:00 【litanyuan】
background
STL Provided in swap Algorithm , Used to exchange the values of two objects , The general implementation method is as follows :
namespace std{
template<typename T>
void swap( T&a,T&b )
{
T temp(a);// Copy structure
a = b;// copy assignment
b = temp;// copy assignment
}
}
Resource management
①. summary
Usually contains resources ( The pointer ) The class of needs to provide a custom copy constructor and copy assignment operator .
②. Class definition
class demoClass
{
public:
demoClass(const string& s = string()) :str(new string(s))
{
cout << " Constructor execution " << endl;
}
demoClass(const demoClass& d):str(new string(*d.str))
{
cout << " The copy constructor executes " << endl;
}
demoClass& operator=(const demoClass& d)
{
cout << " Copy assignment function execution " << endl;
auto newStr = new string(*d.str);// Copy the underlying resources
delete str;// Release old resources
str = newStr;// Assign new values to
return *this;
}
~demoClass()
{
cout << " The destructor executes " << endl;
delete str;
}
private:
string* str;// The pointer
};
③. Default swap operation
default swap The algorithm will execute the copy constructor once and the copy assignment operator twice , It will be more time-consuming for relatively large resources .
int main()
{
demoClass d1 = demoClass("123");
demoClass d2 = demoClass("456");
cout << " perform swap " << endl;
swap(d1, d2);
system("pause");
return 0;
}
④. Custom efficient swap
Corresponding to the class containing larger resources , When exchanging, you can exchange pointers directly . Put custom swap Function is defined as a friend function of a class to access private members .
class demoClass
{
public:
friend void swap(demoClass& d1, demoClass& d2) noexcept;// Defined as friend
/* ditto */
private:
string* str;// The pointer
};
void swap(demoClass& d1, demoClass& d2) noexcept
{
using std::swap;
swap(d1.str, d2.str);// Exchange of pointer
}
int main()
{
demoClass d1 = demoClass("123");
demoClass d2 = demoClass("456");
cout << " perform swap " << endl;
swap(d1, d2);
system("pause");
return 0;
}
边栏推荐
猜你喜欢
剪映+json解析将视频中的声音转换成文本
UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
基於 QEMUv8 搭建 OP-TEE 開發環境
Senior soft test (Information System Project Manager) high frequency test site: project quality management
自定义 swap 函数
View
Clip +json parsing converts the sound in the video into text
pytorch_ Yolox pruning [with code]
AdaViT——自适应选择计算结构的动态网络
Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
随机推荐
金融人士必读书籍系列之六:权益投资(基于cfa考试内容大纲和框架)
Aardio - 通过变量名将变量值整合到一串文本中
Const keyword
Aardio - construct a multi button component with customplus library +plus
Void keyword
BasicVSR_ Plusplus master test videos and pictures
Aardio - does not declare the method of directly passing float values
Should novice programmers memorize code?
2022-07-05 使用tpcc对stonedb进行子查询测试
Web APIs DOM time object
The ceiling of MySQL tutorial. Collect it and take your time
OpenNMS separation database
Aardio - 不声明直接传float数值的方法
config:invalid signature 解决办法和问题排查详解
Windows auzre background operation interface of Microsoft's cloud computing products
如何用程序确认当前系统的存储模式?
General implementation and encapsulation of go diversified timing tasks
three.js绚烂的气泡效果
Sizeof keyword
【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence