STL之list学习(1)
list(链表)容器也是顺序容器的一种,支持快速插入/删除,有头插入,尾插入两种数据输入的方法。不支持下标操作!!!
这里的只是基本的操作,还有一些操作可能以后会继续补充。以后会有list的代码实现!!
1 #include<iostream> 2 #include<list> 3 using namespace std; 4 5 int main( ) 6 { 7 list<int> l1; 8 list<int> l2(l1); 9 list<int> l3(10, 99); 10 list<int> l4(10, 99); 11 cout << "l1的size:" << endl; 12 cout << l1.size() << endl; 13 if( l2.empty() ) 14 cout << "l2为空的!" << endl; 15 else 16 cout << "l2不是空的!" << endl; 17 cout << "输入你想在l1、l2里面出入多少元素:" << endl; 18 int num, m; 19 cin >> num; 20 for( int i = 0; i != num; ++i) 21 { 22 cin >> m; 23 l1.push_back(m); 24 l2.push_front(m); 25 } 26 cout << "l1为尾插入,l2为头插入!!" << endl; 27 cout << "l1的全部元素顺序输出:" << endl; 28 for(list<int>::iterator iter = l1.begin(); iter != l1.end(); ++iter) 29 cout << *iter << " "; 30 cout << endl; 31 cout << "l2的全部元素顺序输出:" << endl; 32 for(list<int>::iterator iter = l2.begin(); iter != l2.end(); ++iter) 33 cout << *iter << " "; 34 cout << endl; 35 list<int>::size_type size = l1.size(); 36 cout << "l1的size:" << endl; 37 //cout << l1.size() << endl; 38 cout << size << endl; 39 cout << "l1可容纳的最多元素个数:" << endl; 40 cout << l1.max_size() << endl; 41 cout << "输出l3的元素:" <<endl; 42 for(list<int>::iterator iter = l3.begin(); iter != l3.end(); ++iter) 43 cout << *iter << " "; 44 cout << endl; 45 int n; 46 cout << "改变l3的大小,输入你想要l3的大小(请输入一个小于10的数,方便比较):" << endl; 47 cin >> n; 48 l3.resize(n); 49 for(list<int>::iterator iter = l3.begin(); iter != l3.end(); ++iter) 50 cout << *iter << " "; 51 cout << endl; 52 cout << "再次改变l3的大小,输入你想要l3的大小(请输入一个大于10的数,方便比较):" << endl; 53 cin >> n; 54 l3.resize(n); 55 for(list<int>::iterator iter = l3.begin(); iter != l3.end(); ++iter) 56 cout << *iter << " "; 57 cout << endl; 58 cout << "再次改变l3的大小,输入你想要l3的大小(请输入一个大于之前n的数,方便比较看resize(n, t)函数的结果):" << endl; 59 cin >> n; 60 cout << "输入一个数,容器所有新添加的元素的值会等于这个数:" << endl; 61 int t; 62 cin >> t; 63 l3.resize(n, t); 64 for(list<int>::iterator iter = l3.begin(); iter != l3.end(); ++iter) 65 cout << *iter << " "; 66 cout << endl; 67 return 0; 68 }