In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use the swap of Cellular 11". In the daily operation, I believe that many people have doubts about how to use the swap of Cellular 11. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the swap of Cellular 11". Next, please follow the editor to study!
Suppose you have the following data classes:
Struct TestData {
TestData (int _ size)
: size (_ size)
{
Data= new int [size]
}
~ TestData () {
If (data! = nullptr) {
Delete data
}
}
TestData (const TestData& d)
{
Size = d.size
If (data! = nullptr) {
Delete data
}
Data = new int [size]
Memcpy (data, d.data, size * sizeof (int))
}
TestData& operator= (const TestData& d)
{
Size = d.size
If (data! = nullptr) {
Delete data
}
Data = new int [size]
Memcpy (data, d.data, size * sizeof (int))
Return * this
}
Int size = 0
Int* data = nullptr
}
This is a simple data class that defines a copy constructor and an assignment operator. They all make deep copies.
Swap before Clearing 11
Let's first take a look at the implementation of swap:
Templatevoidswap (T & a, T & b) {T _ c (a); a _ r _ b; b _ c;}
Let's take a look at what happens with the code below in the example.
When swap calls T C (a), it actually calls the copy constructor, and when the swap code invokes the assignment, it actually calls the assignment operator.
Because the copy constructor and assignment operators contain memory copy operations, which are performed three times, there are three memory copy operations in a swap. This unnecessary memory operation will affect the execution efficiency of C++ in many cases.
Swap after Clearing 11
With the introduction of the concepts of right-value reference and data movement, the code looks like this:
Templatevoidswap (T & a, T & b) {TC (std::move (a)); a=std::move (b); b=std::move (c);}
Because std::move converts variable types to right-value references, TestData has the opportunity to provide the following constructors and assignment operators for right-value references.
TestData (TestData&& d)
: size (d.size)
, data (d.data)
{
D.size = 0
D.data = nullptr
}
TestData& operator= (const TestData&& d)
{
Size = d.size
Data = d.data
Return * this
}
At this point, the study on "how to use the swap of Category 11" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.