当前位置:网站首页>自定义 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;
}
边栏推荐
- Classic sql50 questions
- Barcodex (ActiveX print control) v5.3.0.80 free version
- Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
- Applet system update prompt, and force the applet to restart and use the new version
- Oracle-控制文件及日志文件的管理
- sizeof关键字
- C # realizes crystal report binding data and printing 4-bar code
- Classification, function and usage of MySQL constraints
- 图像的spatial domain 和 frequency domain 图像压缩
- Advantages of link local address in IPv6
猜你喜欢
C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
小常识:保险中的“保全”是什么?
图像的spatial domain 和 frequency domain 图像压缩
PVL EDI 项目案例
HDR image reconstruction from a single exposure using deep CNNs阅读札记
pytorch_YOLOX剪枝【附代码】
Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
剪映+json解析将视频中的声音转换成文本
Installation and use of labelimg
Chapter 4: talk about class loader again
随机推荐
在IPv6中 链路本地地址的优势
Learn the principle of database kernel from Oracle log parsing
Aardio - 封装库时批量处理属性与回调函数的方法
Aardio - 不声明直接传float数值的方法
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
extern关键字
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
0 basic learning C language - digital tube
Management background --2 Classification list
Management background --3, modify classification
Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
Build op-tee development environment based on qemuv8
做接口测试都测什么?有哪些通用测试点?
AI 企业多云存储架构实践 | 深势科技分享
Daily question 1: force deduction: 225: realize stack with queue
Data storage (1)
414. The third largest digital buckle
labelimg的安装与使用
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv