STL——容器(List)list 的賦值操作


list.assign(beg, end);

//將[beg, end)區間中的數據拷貝賦值給本身

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333,444,555 };
 9     list<int> listInt_A(num, num + size(num));
10     list<int> listInt_B;
11 
12     cout << "遍歷 listInt_A:";
13     for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
14     {
15         cout << *it << " ";
16     }
17     cout << endl;
18 
19     listInt_B.assign(++listInt_A.begin(), --listInt_A.end());
20     cout << "遍歷 listInt_B:";
21     for (list<int>::iterator it = listInt_B.begin(); it != listInt_B.end(); it++)
22     {
23         cout << *it << " ";
24     }
25     cout << endl;
26 
27     return 0;
28 }

打印結果:

end()是結束符,但沒有打印出來555,是因為前開后閉,

 

 

 


list.assign(n, elem);

//將n個elem拷貝賦值給本身

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     list<int> listInt_A(5, 888);
 9 
10     cout << "遍歷 listInt_A:";
11     for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     cout << endl;
16 
17     return 0;
18 }

打印結果:

 

 

 


list& operator=(const list& lst);

//重載等號操作符

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333,444,555 };
 9     list<int> listInt_A(num, num + size(num));
10     list<int> listInt_B;
11 
12     cout << "遍歷 listInt_A:";
13     for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
14     {
15         cout << *it << " ";
16     }
17     cout << endl;
18 
19     listInt_B = listInt_A;
20     cout << "遍歷 listInt_B:";
21     for (list<int>::iterator it = listInt_B.begin(); it != listInt_B.end(); it++)
22     {
23         cout << *it << " ";
24     }
25     cout << endl;
26 
27     return 0;
28 }

打印結果:

 

 

 


list.swap(lst);

//將lst與本身的元素互換

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333,444,555 };
 9     list<int> listInt_A(num, num + size(num));
10     list<int> listInt_B(5, 888);
11 
12     cout << "互換前 遍歷 listInt_A:";
13     for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
14     {
15         cout << *it << " ";
16     }
17     cout << endl;
18     cout << "互換前 遍歷 listInt_B:";
19     for (list<int>::iterator it = listInt_B.begin(); it != listInt_B.end(); it++)
20     {
21         cout << *it << " ";
22     }
23     cout << endl;
24 
25     //互換
26     listInt_A.swap(listInt_B);
27     cout << "互換后 遍歷 listInt_A:";
28     for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
29     {
30         cout << *it << " ";
31     }
32     cout << endl;
33     cout << "互換后 遍歷 listInt_B:";
34     for (list<int>::iterator it = listInt_B.begin(); it != listInt_B.end(); it++)
35     {
36         cout << *it << " ";
37     }
38     cout << endl;
39 
40     return 0;
41 }

打印結果:

 

 

 

 

 

 

 

===================================================================================================================


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM