STL——容器(List)List 的數據元素插入和刪除操作


push_back(elem);

//在容器尾部加入一個元素

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

打印結果:

 

 

 

pop_back();

//刪除容器中最后一個元素

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

打印結果:

 

 

 

 

push_front(elem);

//在容器開頭插入一個元素

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

打印結果:

 

 

 

 

pop_front();

//從容器開頭移除第一個元素

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

打印結果:

 

 

 

 

insert(pos, elem);

//在pos位置插elem元素的拷貝,返回新數據的位置

這里需要注意一點,list 不可以隨機存取元素,所以不支持 at.(position)函數與[]操作符。可以對其迭代器執行++和--,但是不能這樣操作迭代器:it + 3

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333 };
 9     list<int> listInt(num, num + size(num));
10     cout << "初始遍歷 listInt:";
11     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     cout << endl;
16 
17 
18     cout << "insert 后,用 insert 的返回值遍歷 listInt:";
19     for (list<int>::iterator it = listInt.insert(++listInt.begin(), 888); it != listInt.end(); it++)
20     {
21         cout << *it << " ";
22     }
23     cout << endl;
24     cout << "最終遍歷 listInt:";
25     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
26     {
27         cout << *it << " ";
28     }
29 
30     return 0;
31 }

打印結果:

 

 

 

 

insert(pos, n, elem);

//在pos位置插入n個elem數據,返回新數據的第一個數據的位置(這個有沒有返回值是編譯器版本決定,早起版本的編譯器沒有返回值)

這里需要注意一點,list 不可以隨機存取元素,所以不支持 at.(position)函數與[]操作符。可以對其迭代器執行++和--,但是不能這樣操作迭代器:it + 3

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333 };
 9     list<int> listInt(num, num + size(num));
10     cout << "初始遍歷 listInt:";
11     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     cout << endl;
16 
17     
18     cout << "insert 后,用 insert 的返回值遍歷 listInt:";
19     for (list<int>::iterator it = listInt.insert(++listInt.begin(), 2, 888); it != listInt.end(); it++)
20     {
21         cout << *it << " ";
22     }
23     cout << endl;
24     cout << "最終遍歷 listInt:";
25     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
26     {
27         cout << *it << " ";
28     }
29 
30     return 0;
31 }

打印結果:

 

 

 

 

 

insert(pos, beg, end);

//在pos位置插入[beg,end)區間的數據,下面代碼我舉兩種使用方法,一種是使用迭代器插入,另一種是插入數組

這里需要注意一點,list 不可以隨機存取元素,所以不支持 at.(position)函數與[]操作符。可以對其迭代器執行++和--,但是不能這樣操作迭代器:it + 3

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333 };
 9     int num_1[] = { 666,777,888 };
10     list<int> listInt(num, num + size(num));
11     list<int> listInt_A(2, 666);
12     cout << "初始遍歷 listInt:";
13     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
14     {
15         cout << *it << " ";
16     }
17     cout << endl;
18 
19     
20     cout << "insert 后,用 insert 的返回值遍歷 listInt:";
21     for (list<int>::iterator it = listInt.insert(++listInt.begin(), listInt_A.begin(), listInt_A.end()); it != listInt.end(); it++)
22     {
23         cout << *it << " ";
24     }
25 
26     cout << endl;
27     cout << "最終遍歷 listInt:";
28     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
29     {
30         cout << *it << " ";
31     }
32     cout << endl << endl;
33 
34 
35     //當然這里也可以插入數組
36     cout << "使用 insert 插入數組,然后用 insert 的返回值遍歷 listInt:" << endl;
37     for (list<int>::iterator it = listInt.insert(++listInt.begin(), num_1, num_1 + size(num_1)); it != listInt.end(); it++)
38     {
39         cout << *it << " ";
40     }
41 
42     cout << endl;
43     cout << "最終遍歷 listInt:";
44     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
45     {
46         cout << *it << " ";
47     }
48 
49     return 0;
50 }

打印結果:

 

 

 

 

clear();

//移除容器的所有數據

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

打印結果:

 

 

 

 

erase(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,666 };
 9     list<int> listInt(num, num + size(num));
10     cout << "erase 前遍歷 listInt:";
11     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     cout << endl;
16 
17     
18     cout << "erase 后,用其返回值遍歷 listInt:";
19     for (list<int>::iterator it = listInt.erase(++listInt.begin(), --listInt.end()); it != listInt.end(); it++)
20     {
21         cout << *it << " ";
22     }
23 
24     cout << endl;
25     cout << "erase 后遍歷 listInt:";
26     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
27     {
28         cout << *it << " ";
29     }
30 
31     return 0;
32 }

打印結果:

 

 

 

 

erase(pos);

//刪除pos位置的數據,返回下一個數據的位置

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

打印結果:

 

 

 

remove(elem);

//刪除容器中所有與elem值匹配的元素

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

打印結果:

 還有一種遍歷刪除法:

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

打印結果:

 

 

 

 

 

 

 

 

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

 


免責聲明!

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



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