STL容器使用DEMO-list

Code:
  1. //   
  2. //  CopyRight(c) 2009, YOYO, All Rights Reserved.   
  3. //  Author: LIN YiQian   
  4. //  Created: 2009/08/24   
  5. //  Describe: STL list 使用DEMO   
  6. //   
  7. #include <iostream>   
  8. #include <list>   
  9.   
  10. using namespace std;   
  11.   
  12. typedef list<int> INT_LST;   
  13.   
  14. // 打印List   
  15. void PrintList(INT_LST lstInt)   
  16. {   
  17.     for (INT_LST::iterator lstIter = lstInt.begin(); lstIter != lstInt.end(); ++lstIter)   
  18.     {   
  19.         cout << *lstIter;   
  20.     }   
  21.     cout << endl;   
  22. }   
  23.   
  24. // 逆向打印List   
  25. void PrintListReverse(INT_LST lstInt)   
  26. {   
  27.     for (INT_LST::reverse_iterator lstRIter = lstInt.rbegin(); lstRIter != lstInt.rend(); ++lstRIter)   
  28.     {   
  29.         cout << *lstRIter;   
  30.     }   
  31.     cout << endl;   
  32. }   
  33.   
  34. void main(void)   
  35. {   
  36.     INT_LST lstInt1;   
  37.     cout << "lstInt1: "; PrintList(lstInt1);   
  38.   
  39.     //  push_front() & push_back()   
  40.     {   
  41.         lstInt1.push_front(3);   
  42.         lstInt1.push_front(2);   
  43.         lstInt1.push_back(8);   
  44.         lstInt1.push_back(9);   
  45.         lstInt1.push_back(7);   
  46.         cout << "lstInt1 after Push: "; PrintList(lstInt1);   
  47.     }   
  48.   
  49.     //  insert()   
  50.     {   
  51.         lstInt1.insert(++ ++lstInt1.begin(), 5);   
  52.         lstInt1.insert(++ lstInt1.begin(), 6);   
  53.         cout << "lstInt1 after Insert: "; PrintList(lstInt1);   
  54.     }   
  55.   
  56.     //  sort()   
  57.     {   
  58.         lstInt1.sort();   
  59.         cout << "lstInt1 after Sort: "; PrintList(lstInt1);   
  60.     }   
  61.   
  62.     //  size() &  max_size()   
  63.     {   
  64.         cout << "lstInt1 size: " << lstInt1.size() << endl;   
  65.         cout << "lstInt1 max_size: " << lstInt1.max_size() << endl;   
  66.     }   
  67.   
  68.     //  empty()   
  69.     {   
  70.         cout << "lstInt1 Empty?: " << boolalpha << lstInt1.empty() << endl;   
  71.     }   
  72.   
  73.     //  reverse()   
  74.     {   
  75.         cout << "lstInt1 reverse: "; PrintListReverse(lstInt1);   
  76.     }   
  77.   
  78.     //  front() & back()   
  79.     {   
  80.         cout << "lstInt1 front: " << lstInt1.front() << endl;   
  81.         cout << "lstInt1 back: " << lstInt1.back() << endl;   
  82.     }   
  83.   
  84.     INT_LST lstInt2(7, 9);   
  85.     cout << "lstInt2: "; PrintList(lstInt2);   
  86.   
  87.     INT_LST lstInt3(lstInt1);   
  88.     cout << "lstInt3: "; PrintList(lstInt3);   
  89.   
  90.     //  pop_front() & pop_back()   
  91.     {   
  92.         lstInt3.pop_front();   
  93.         lstInt3.pop_back();   
  94.         cout << "lstInt3 after Pop: "; PrintList(lstInt3);   
  95.     }   
  96.   
  97.     //  erase()   
  98.     {   
  99.         lstInt3.erase(++lstInt3.begin());   
  100.         cout << "lstInt3 after Erase: "; PrintList(lstInt3);   
  101.   
  102.         lstInt3.erase(++lstInt3.begin(), --lstInt3.end());   
  103.         cout << "lstInt3 after Erase: "; PrintList(lstInt3);   
  104.     }   
  105.   
  106.     INT_LST lstInt4(lstInt2.begin(), lstInt2.end());   
  107.     cout << "lstInt4: "; PrintList(lstInt4);   
  108.   
  109.     //  assign()   
  110.     {   
  111.         lstInt4.assign(9, 2);   
  112.         cout << "lstInt4 after Assign: "; PrintList(lstInt4);   
  113.     }   
  114.   
  115.     //  operator cmp   
  116.     {   
  117.         cout << "lstInt4 == lstInt2?: " << boolalpha << (lstInt2 == lstInt4) << endl;   
  118.         cout << "lstInt4 <= lstInt2?: " << boolalpha << (lstInt4 <= lstInt2) << endl;   
  119.     }   
  120.   
  121.     //  splice()   
  122.     {   
  123.         lstInt4.splice(++lstInt4.begin(), lstInt2, ++ ++lstInt2.begin(), --lstInt2.end());   
  124.         cout << "lstInt4 after Splice: "; PrintList(lstInt4);   
  125.         cout << "lstInt2 after Splice: "; PrintList(lstInt2);   
  126.   
  127.         lstInt4.splice(lstInt4.begin(), lstInt1, ++ lstInt1.begin());   
  128.         cout << "lstInt4 after Splice: "; PrintList(lstInt4);   
  129.         cout << "lstInt1 after Splice: "; PrintList(lstInt1);   
  130.   
  131.         lstInt4.splice(lstInt4.begin(), lstInt3);   
  132.         cout << "lstInt4 after Splice: "; PrintList(lstInt4);   
  133.         cout << "lstInt3 after Splice: "; PrintList(lstInt3);   
  134.     }   
  135.   
  136.     //  swap()   
  137.     {   
  138.         lstInt4.swap(lstInt1);   
  139.         cout << "lstInt4 after Swap: "; PrintList(lstInt4);   
  140.         cout << "lstInt1 after Swap: "; PrintList(lstInt1);   
  141.     }   
  142.   
  143.     //  clear()   
  144.     {   
  145.         lstInt4.clear();   
  146.         cout << "lstInt4 after Clear: "; PrintList(lstInt4);   
  147.         cout << "lstInt4 size: " << lstInt4.size() << endl;   
  148.         cout << "lstInt4 Empty?: " << boolalpha << lstInt4.empty() << endl;   
  149.     }   
  150.   
  151.     system("pause");   
  152. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值