C STL list對物件變數操作的一些總結

2021-07-17 03:55:10 字數 2807 閱讀 1744

list中儲存的是物件(lsit)本身的話,在push_pack的時候,會呼叫copy賦值建構函式,在呼叫erase時,會呼叫析構函式,list變數結束時,會呼叫其中所有物件的析構函式。這是自己現階段的理解。有深入會後續補充。

下面是**舉例:

#include

#include

#include

#include

#include

using namespace std;

class person

person(const person& p)

~person()

void play()

public:

string  m_name;

int     m_age; };

void listtest(list& plistperson)

list::iterator curiter = curlistperson.begin();

for(; curiter != curlistperson.end(); ++curiter)

}printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");

list::iterator curiter1 = curlistperson.begin();

for(; curiter1 != curlistperson.end(); ++curiter1)}

int main()

printf("-----------------------------------------\n");

list::iterator piter = plistperson.begin();

for(; piter != plistperson.end(); ++piter)

listtest(plistperson);

printf("***************************************\n");

cout << "listtest end..." << endl;

return 0;  }

執行結果:

listtest begin..

name0:20 created!

name1:21 created!

name2:22 created!

name3:23 created!

name4:24 created!

name5:25 created!

name6:26 created!

name7:27 created!

name8:28 created!

name9:29 created!

-----------------------------------------

name0:20

name1:21

name2:22

name3:23

name4:24

name5:25

name6:26

name7:27

name8:28

name9:29

+++++++++++++++++++++++++++++++

name0:20 copy!

name0:20 copy!

name0:20 destroy!

name1:21 copy!

name1:21 copy!

name1:21 destroy!

name2:22 copy!

name2:22 copy!

name2:22 destroy!

name3:23 copy!

name3:23 copy!

name3:23 destroy!

name4:24 copy!

name4:24 copy!

name4:24 destroy!

name5:25 copy!

name5:25 copy!

name5:25 destroy!

name6:26 copy!

name6:26 copy!

name6:26 destroy!

name7:27 copy!

name7:27 copy!

name7:27 destroy!

name8:28 copy!

name8:28 copy!

name8:28 destroy!

name9:29 copy!

name9:29 copy!

name9:29 destroy!

name0:20

name0:20 destroy!

name2:22

name2:22 destroy!

name4:24

name4:24 destroy!

name6:26

name6:26 destroy!

name8:28

name8:28 destroy!

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

name1:21

name3:23

name5:25

name7:27

name9:29

name1:21 destroy!

name3:23 destroy!

name5:25 destroy!

name7:27 destroy!

name9:29 destroy!

***************************************

listtest end...

js對物件操作

遍歷物件 for let key,value of object.entries fruits 也可以 遍歷li 阿薩li 時代li 地方li ul body const lis document.queryselectorall li for let li of lis const names a...

21C STL list 基本操作

include include using namespace std intmain include include using namespace std template typename t void display const t a intmain include include usi...

mysql面對物件 面對物件資料庫

物件導向的方式訪問資料庫 造物件 db new mysqli localhost root 123 mydb 判斷連線是否出錯 if mysqli connect error mysqli connect error or die 連線失敗!寫sql語句 sql insert into info v...