Internal implementation of push_back (object) Saving object in list in STL

Source: Internet
Author: User

Internal implementation of push_back (object) Saving object in list in STL

1. In the container, a copy of the object is stored

  

#include <iostream>#include<list>using namespacestd;classa{inti; Static intnum; Public: A (): I (0) {cout<<"A ()"<<endl; Num + +;} A (intII): I (ii) {cout<<"A (int)"<<endl; Num + +;} ~a () {cout<<"~a"<<Endl;} A (Consta&a) {i=A.I; cout<<"A (const a&)"<<Endl; Num++; } A&operator=(Consta&a) {cout<<"operator="<<Endl; I=A.I; return* This; }    voidprint () {cout<<i<<Endl; }    voidPrintn () {cout<<"num ="<<num<<Endl; }};intA::num =0;intMain () {//A (1); //a b = A; //A C; //C = A; //C.print (); //a.printn ();List<a>Li; Li.push_back (A (2)); List<a>::iterator it =Li.begin (); (*it). PRINTN ();} Output result: A (int)//a2 Temporary object
A (Consta&)//Li.push_back (A (2)) calls its copy constructor~a//leave temporary object scope A call its destructor num=2~A//program end from the output can be seen in the list is a copy of the object.

Internal implementation of push_back (object) Saving object in list in STL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.