STL-list 链表

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     // list可以在头部和尾部插入和删除元素
 9     // 不能随机访问元素,迭代器只能++,不能一次性跳转
10     list<int> L;
11     //L.push(1);
12     L.push_back(1);
13     L.push_front(3);
14     L.push_back(5);
15 
16     for(list<int>::iterator it=L.begin();it!=L.end();++it)
17     {
18         // it=L.begin()+1也不行
19         cout<<*it<<' ';
20         //it+=1;
21         //it=it+1;
22     }
23     cout<<endl;
24 
25 
26     //删除
27     //erase
28     list<int>::iterator it=L.begin();
29     ++it;
30     L.erase(it);
31 
32     for(list<int>::iterator it=L.begin();it!=L.end();++it)
33     {
34         cout<<*it<<' ';
35     }
36     cout<<endl;
37 
38     // remove ->值删除
39     L.remove(1);
40     for(list<int>::iterator it=L.begin();it!=L.end();++it)
41     {
42         cout<<*it<<' ';
43     }
44     cout<<endl;
45     L.remove(3);
46     for(list<int>::iterator it=L.begin();it!=L.end();++it)
47     {
48         cout<<*it<<' ';
49     }
50     cout<<endl;
51 
52     return 0;
53 }

猜你喜欢

转载自www.cnblogs.com/jishuren/p/12238943.html