当前位置:网站首页>自定义 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;
}
边栏推荐
- 图像的spatial domain 和 frequency domain 图像压缩
- Senior soft test (Information System Project Manager) high frequency test site: project quality management
- Memorabilia of domestic database in June 2022 - ink Sky Wheel
- Learn the principle of database kernel from Oracle log parsing
- Web APIs DOM time object
- 数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
- 【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真
- Spatial domain and frequency domain image compression of images
- Assembly and interface technology experiment 5-8259 interrupt experiment
- return 关键字
猜你喜欢
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
Installation and use of labelimg
AdaViT——自适应选择计算结构的动态网络
数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
Should novice programmers memorize code?
C#實現水晶報錶綁定數據並實現打印4-條形碼
2500个常用中文字符 + 130常用中英文字符
ZABBIX proxy server and ZABBIX SNMP monitoring
手写ABA遇到的坑
随机推荐
Shortcut keys in the terminal
网络基础入门理解
pytorch_ Yolox pruning [with code]
CCNA-思科网络 EIGRP协议
空结构体多大?
Build op-tee development environment based on qemuv8
Inno Setup 打包及签名指南
Learn the principle of database kernel from Oracle log parsing
Lora sync word settings
Pit encountered by handwritten ABA
What are the interface tests? What are the general test points?
Aardio - 通过变量名将变量值整合到一串文本中
做接口测试都测什么?有哪些通用测试点?
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
Management background --4, delete classification
2500个常用中文字符 + 130常用中英文字符
Support multiple API versions in flask
void关键字
Data storage (1)
Installation and use of labelimg