当前位置:网站首页>自定义 swap 函数
自定义 swap 函数
2022-07-06 14:53:00 【litanyuan】
背景
STL 中提供了 swap 算法,用于交换两个对象的值,其一般实现方法如下:
namespace std{
template<typename T>
void swap( T&a,T&b )
{
T temp(a);//拷贝构造
a = b;//拷贝赋值
b = temp;//拷贝赋值
}
}
资源管理类
①.概述
通常包含资源(指针)的类需要提供自定义的拷贝构造函数及拷贝赋值运算符。
②.类定义
class demoClass
{
public:
demoClass(const string& s = string()) :str(new string(s))
{
cout << "构造函数执行" << endl;
}
demoClass(const demoClass& d):str(new string(*d.str))
{
cout << "拷贝构造函数执行" << endl;
}
demoClass& operator=(const demoClass& d)
{
cout << "拷贝赋值函数执行" << endl;
auto newStr = new string(*d.str);//拷贝底层资源
delete str;//释放旧资源
str = newStr;//赋新值
return *this;
}
~demoClass()
{
cout << "析构函数执行" << endl;
delete str;
}
private:
string* str;//指针
};
③.默认 swap 操作
默认的 swap 算法会执行一次拷贝构造函数及两次拷贝赋值运算符,若对于比较大的资源会比较耗时。
int main()
{
demoClass d1 = demoClass("123");
demoClass d2 = demoClass("456");
cout << "执行 swap " << endl;
swap(d1, d2);
system("pause");
return 0;
}

④.自定义高效 swap
对应包含较大资源的类,交换时直接交换指针即可。把自定义的 swap 函数定义为类的友元函数以访问私有成员。
class demoClass
{
public:
friend void swap(demoClass& d1, demoClass& d2) noexcept;//定义为友元
/* 同上 */
private:
string* str;//指针
};
void swap(demoClass& d1, demoClass& d2) noexcept
{
using std::swap;
swap(d1.str, d2.str);//交换指针
}
int main()
{
demoClass d1 = demoClass("123");
demoClass d2 = demoClass("456");
cout << "执行 swap " << endl;
swap(d1, d2);
system("pause");
return 0;
}


边栏推荐
- Barcodex (ActiveX print control) v5.3.0.80 free version
- Chapter 3: detailed explanation of class loading process (class life cycle)
- Self made j-flash burning tool -- QT calls jlinkarm DLL mode
- Build op-tee development environment based on qemuv8
- 剪映+json解析将视频中的声音转换成文本
- Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
- 2022-07-05 stonedb的子查询处理解析耗时分析
- 2021 geometry deep learning master Michael Bronstein long article analysis
- Web APIs DOM 时间对象
- Maximum product of three numbers in question 628 of Li Kou
猜你喜欢

Aardio - 封装库时批量处理属性与回调函数的方法

如何用程序确认当前系统的存储模式?

NPDP certification | how do product managers communicate across functions / teams?

leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
![[linear algebra] determinant of order 1.3 n](/img/6e/54f3a994fc4c2c10c1036bee6715e8.gif)
[linear algebra] determinant of order 1.3 n

C#实现水晶报表绑定数据并实现打印4-条形码
![[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation](/img/2b/15b3d831bba6aa772ad83f3ac91d23.png)
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation

Clip +json parsing converts the sound in the video into text
Learn the principle of database kernel from Oracle log parsing

2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9
随机推荐
Support multiple API versions in flask
Daily question 1: force deduction: 225: realize stack with queue
HDR image reconstruction from a single exposure using deep CNN reading notes
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
Solve project cross domain problems
手写ABA遇到的坑
case 关键字后面的值有什么要求吗?
Classic sql50 questions
MySQL数据库基本操作-DML
雅思口语的具体步骤和时间安排是什么样的?
中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
Management background --3, modify classification
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
2500 common Chinese characters + 130 common Chinese and English characters
Installation and use of labelimg
[sdx62] wcn685x will bdwlan Bin and bdwlan Txt mutual conversion operation method
General implementation and encapsulation of go diversified timing tasks
Inno setup packaging and signing Guide
Web APIs DOM 时间对象
Self made j-flash burning tool -- QT calls jlinkarm DLL mode