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


边栏推荐
- [leetcode] 19. Delete the penultimate node of the linked list
- Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
- Const keyword
- What are the specific steps and schedule of IELTS speaking?
- Web APIs DOM time object
- 在IPv6中 链路本地地址的优势
- Financial professionals must read book series 6: equity investment (based on the outline and framework of the CFA exam)
- SQL Server生成自增序号
- 0 basic learning C language - interrupt
- 雅思口语的具体步骤和时间安排是什么样的?
猜你喜欢

二分图判定

Installation and use of labelimg

Mysql database basic operations DML

0 basic learning C language - interrupt

Aardio - 不声明直接传float数值的方法

Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)

Rust knowledge mind map XMIND

MySQL数据库基本操作-DML

Config:invalid signature solution and troubleshooting details

(十八)LCD1602实验
随机推荐
The SQL response is slow. What are your troubleshooting ideas?
0 basic learning C language - digital tube
SQL server generates auto increment sequence number
Chapter 19 using work queue manager (2)
signed、unsigned关键字
树的先序中序后序遍历
Aardio - 利用customPlus库+plus构造一个多按钮组件
手写ABA遇到的坑
Windows Auzre 微软的云计算产品的后台操作界面
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
extern关键字
Comparison between variable and "zero value"
Improving Multimodal Accuracy Through Modality Pre-training and Attention
【无标题】
变量与“零值”的比较
Uniapp setting background image effect demo (sorting)
Web APIs DOM time object
Mysql database basic operations DML
雅思口语的具体步骤和时间安排是什么样的?
Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing