List

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<list>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     list<int>l;         //创建一个空的list
 9     l.assign(4,3);      //分配值
10     list<int>L1;
11     L1=l;
12     cout<<l.front()<<endl;
13     cout<<l.back()<<endl;
14     list<int>::iterator ptr;
15     for(ptr=l.begin();ptr!=l.end();++ptr)
16     {
17         cout<<*ptr<<" ";
18     }
19     cout<<endl;
20     l.pop_back();
21     l.pop_front();
22     l.push_back(2);
23     l.push_front(1);
24     l.erase(l.begin());   //删除该地址的元素
25     l.remove(3);          //remove,把是3的全部删除
26     for(ptr=l.begin();ptr!=l.end();++ptr)
27     {
28         cout<<*ptr<<" ";
29     }
30     cout<<endl;
31     for(int i=0;i<5;i++)
32     {
33         l.push_back(i);
34     }
35     l.sort();
36     for(ptr=l.begin();ptr!=l.end();++ptr)
37     {
38         cout<<*ptr<<" ";
39     }
40     cout<<endl;
41     l.sort(greater<int>());
42     for(ptr=l.begin();ptr!=l.end();++ptr)
43     {
44         cout<<*ptr<<" ";
45     }
46     cout<<endl;
47     list<int>l1(5,2);
48     for(ptr=l1.begin();ptr!=l1.end();++ptr)
49     {
50         cout<<*ptr<<" ";
51     }
52     cout<<endl;
53     l1.merge(l,greater<int>());
54     for(ptr=l1.begin();ptr!=l1.end();++ptr)
55     {
56         cout<<*ptr<<" ";
57     }
58     l1.swap(l);
59     return 0;
60 }

 

posted on 2013-07-12 15:18  张狂不年轻°  阅读(174)  评论(0)    收藏  举报