当前位置:网站首页>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;
}


边栏推荐
猜你喜欢

手写ABA遇到的坑

That's why you can't understand recursion

LeetCode 练习——剑指 Offer 26. 树的子结构

0 basic learning C language - digital tube

Clip +json parsing converts the sound in the video into text

视图(view)

Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem

View

leetcode:面试题 17.24. 子矩阵最大累加和(待研究)

软考高级(信息系统项目管理师)高频考点:项目质量管理
随机推荐
(18) LCD1602 experiment
POJ 1258 Agri-Net
Extern keyword
金融人士必读书籍系列之六:权益投资(基于cfa考试内容大纲和框架)
three. JS gorgeous bubble effect
POJ 1094 sorting it all out
自定义 swap 函数
Attack and defense world ditf Misc
Comparison between variable and "zero value"
如何实现文字动画效果
BasicVSR_ Plusplus master test videos and pictures
Aardio - 利用customPlus库+plus构造一个多按钮组件
Daily question 1: force deduction: 225: realize stack with queue
void关键字
OpenCV VideoCapture. Get() parameter details
BasicVSR_PlusPlus-master测试视频、图片
如何用程序确认当前系统的存储模式?
Puppeteer连接已有Chrome浏览器
树的先序中序后序遍历
Jafka来源分析——Processor