Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

An example Analysis of the emplace members of the Craft 11 Container

Shulou Source: shulou.com Published: 2022-06-01 19:25:22 10月06日 Update

This article mainly introduces the "example analysis of the emplace members of the Craft 11 container". In the daily operation, I believe that many people have doubts in the example analysis of the emplace members of the container. The editor has consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of the emplace members of the container. Next, please follow the editor to study!

Consider the following Rect classes:

Struct Rect

{

Rect (int l, int t, int r, int b)

: left {l}, top {t}

, right {r}, bottom {b}

{}

Int left

Int top

Int right

Int bottom

}

When you need to add a Rect object to a container, the code looks something like this:

Std::list rlist

Rlist.push_front (Rect (10,10,20,20))

When you call push_front, you first construct a temporary Rect object and pass it to the push_front method, and then, inside the push_front, you copy a Rect object and add it to the container. A create action and a copy action occur throughout the process to add the contents of the object to the list.

Emplace member

In order to reduce the number of copy actions, of course, you can use the right value to refer to the parameter's member function. In addition, emplace membership 11 provides another method: members of the client. Using this member, you can directly pass the parameters used to generate the object, and leave the creation process of the object to the container to execute:

Std::list rlist

Rlist.emplace_front (10, 10, 20, 20)

The usage is very simple, as long as the parameters are the same as those of the element constructor.

In addition to emplace_front, clipping 11 also provides emplace and emplace_bak methods, corresponding to insert and push_back methods, respectively.

Emplace semantics

In order to better understand and explain emplace, first go to Baidu to check, the results are as follows:

At this point, the study on the "example analysis of emplace members of Clear11 container" 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!

Tags: Members containers objects methods analysis parameters learning actions functions copies more help utility same next code elements whole process content articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft macOS NVidia Shulou Information MariaDB