View Code
1 #include <iostream> 2 #include<list> 3 4 using namespace std; 5 6 void PrintListContent(const list<int>& listInput); 7 8 int main() 9 {10 11 12 13 list<int> a;14 list<int> b;15 list<int>::iterator iter;16 17 b.push_back(100);18 b.push_back(200);19 b.push_back(300);20 b.push_back(400);21 b.push_back(500);22 23 24 PrintListContent(b);25 26 cout<< endl;27 28 a.push_front(4);29 a.push_front(3);30 a.push_front(2);31 a.push_front(1);32 33 a.push_back(5);34 35 36 37 iter = a.begin();38 ++iter;39 40 41 a.insert(iter,10);//开头的前面插入了1042 ++iter;43 44 a.insert(iter,4,20);//在后端插入4个2045 46 a.insert(iter,++b.begin(),--b.end());//把b中的数据插入a,从a的开始处或结尾处或中间插入47 48 49 50 //显示链表中数据不能用下标 和deque&vector区分开,只能用迭代器51 52 53 PrintListContent(a);54 55 return 0;56 57 58 }59 60 void PrintListContent(const list<int>& listInput)61 {62 std::list<int>::const_iterator iter;63 for(iter = listInput.begin(); iter != listInput.end(); ++iter)64 cout << *iter <<endl;65 66 67 }
来源:https://www.cnblogs.com/uniquews/archive/2012/03/27/2420410.html