当前位置:网站首页>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;
}
边栏推荐
- Aardio - 不声明直接传float数值的方法
- NPM cannot install sharp
- OpenCV VideoCapture. Get() parameter details
- 将MySQL的表数据纯净方式导出
- Senior soft test (Information System Project Manager) high frequency test site: project quality management
- Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
- pytorch_ Yolox pruning [with code]
- Installation and use of labelimg
- uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)
- OpenNMS分离数据库
猜你喜欢
Mysql 身份认证绕过漏洞(CVE-2012-2122)
Aardio - 不声明直接传float数值的方法
Config:invalid signature solution and troubleshooting details
Should novice programmers memorize code?
Senior soft test (Information System Project Manager) high frequency test site: project quality management
二分图判定
(十八)LCD1602实验
Clip +json parsing converts the sound in the video into text
Aardio - 利用customPlus库+plus构造一个多按钮组件
Attack and defense world miscall
随机推荐
OpenNMS分离数据库
ThreadLocal详解
动作捕捉用于蛇运动分析及蛇形机器人开发
Is there any requirement for the value after the case keyword?
Gd32f4xx serial port receive interrupt and idle interrupt configuration
qt quick项目offscreen模式下崩溃的问题处理
柔性数组到底如何使用呢?
Plafond du tutoriel MySQL, bien collecté, regardez lentement
NPDP认证|产品经理如何跨职能/跨团队沟通?
QT信号和槽
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
rust知识思维导图xmind
Windows Auzre 微软的云计算产品的后台操作界面
How to confirm the storage mode of the current system by program?
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
AdaViT——自适应选择计算结构的动态网络
How to use flexible arrays?
OpenNMS separation database
【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
NPM cannot install sharp