STL标准模板库 --- list

C++list的使用总结及常用list操作

一、List定义:

List是stl实现的双向链表,与向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢。使用时需要添加头文件

#include <list>

二、List定义和初始化:

    list<int>lst1;          //创建空list

    list<int> lst2(5);       //创建含有5个元素的list

    list<int>lst3(3,2);  //创建含有3个元素的list

    list<int>lst4(lst2);    //使用lst2初始化lst4

    list<int>lst5(lst2.begin(),lst2.end());  //同lst4

三、List常用操作函数:

Lst1.assign() 给list赋值 
Lst1.back() 返回最后一个元素 
Lst1.begin() 返回指向第一个元素的迭代器 
Lst1.clear() 删除所有元素 
Lst1.empty() 如果list是空的则返回true 
Lst1.end() 返回末尾的迭代器 
Lst1.erase() 删除一个元素 
Lst1.front() 返回第一个元素 
Lst1.get_allocator() 返回list的配置器 
Lst1.insert() 插入一个元素到list中 
Lst1.max_size() 返回list能容纳的最大元素数量 
Lst1.merge() 合并两个list 
Lst1.pop_back() 删除最后一个元素 
Lst1.pop_front() 删除第一个元素 
Lst1.push_back() 在list的末尾添加一个元素 
Lst1.push_front() 在list的头部添加一个元素 
Lst1.rbegin() 返回指向第一个元素的逆向迭代器 
Lst1.remove() 从list删除元素 
Lst1.remove_if() 按指定条件删除元素 
Lst1.rend() 指向list末尾的逆向迭代器 
Lst1.resize() 改变list的大小 
Lst1.reverse() 把list的元素倒转 
Lst1.size() 返回list中的元素个数 
Lst1.sort() 给list排序 
Lst1.splice() 合并两个list 
Lst1.swap() 交换两个list 
Lst1.unique() 删除list中重复的元素

四、List使用示例:

示例1:遍历List

    //迭代器法

  


 
 
  1. for( list< int>::const_iteratoriter = lst1.begin();iter != lst1.end();iter++)
  2. {
  3. cout<<*iter;
  4. }
  5. cout<< endl;

示例2:


 
 
  1. #include <iostream>
  2. #include <list>
  3. #include <numeric>
  4. #include <algorithm>
  5. #include <windows.h>
  6. using namespace std;
  7. typedef list< int> LISTINT;
  8. typedef list< int> LISTCHAR;
  9. void main()
  10. {
  11. //用LISTINT创建一个list对象
  12. LISTINT listOne;
  13. //声明i为迭代器
  14. LISTINT::iterator i;
  15. listOne.push_front( 3);
  16. listOne.push_front( 2);
  17. listOne.push_front( 1);
  18. listOne.push_back( 4);
  19. listOne.push_back( 5);
  20. listOne.push_back( 6);
  21. cout << "listOne.begin()--- listOne.end():" << endl;
  22. for (i = listOne.begin(); i != listOne.end(); ++i)
  23. cout << *i << " ";
  24. cout << endl;
  25. LISTINT::reverse_iterator ir;
  26. cout << "listOne.rbegin()---listOne.rend():" << endl;
  27. for (ir = listOne.rbegin(); ir != listOne.rend(); ir++) {
  28. cout << *ir << " ";
  29. }
  30. cout << endl;
  31. int result = accumulate(listOne.begin(), listOne.end(), 0);
  32. cout << "Sum=" << result << endl;
  33. cout << "------------------" << endl;
  34. //用LISTCHAR创建一个list对象
  35. LISTCHAR listTwo;
  36. //声明i为迭代器
  37. LISTCHAR::iterator j;
  38. listTwo.push_front( 'C');
  39. listTwo.push_front( 'B');
  40. listTwo.push_front( 'A');
  41. listTwo.push_back( 'D');
  42. listTwo.push_back( 'E');
  43. listTwo.push_back( 'F');
  44. cout << "listTwo.begin()---listTwo.end():" << endl;
  45. for (j = listTwo.begin(); j != listTwo.end(); ++j)
  46. cout << char(*j) << " ";
  47. cout << endl;
  48. j = max_element(listTwo.begin(), listTwo.end());
  49. cout << "The maximum element in listTwo is: " << char(*j) << endl;
  50. Sleep( 10000);
  51. }



 
 
  1. #include <iostream>
  2. #include <list>
  3. #include <windows.h>
  4. using namespace std;
  5. typedef list<int> INTLIST;
  6. //从前向后显示list队列的全部元素
  7. void put_list(INTLIST list, char *name)
  8. {
  9. INTLIST::iterator plist;
  10. cout << "The contents of " << name << " : ";
  11. for (plist = list.begin(); plist != list.end(); plist++)
  12. cout << *plist << " ";
  13. cout << endl;
  14. }
  15. //测试list容器的功能
  16. void main(void)
  17. {
  18. //list1对象初始为空
  19. INTLIST list1;
  20. INTLIST list2( 5, 1);
  21. INTLIST list3(list2.begin(), --list2.end());
  22. //声明一个名为i的双向迭代器
  23. INTLIST::iterator i;
  24. put_list(list1, "list1");
  25. put_list(list2, "list2");
  26. put_list(list3, "list3");
  27. list1.push_back( 7);
  28. list1.push_back( 8);
  29. cout << "list1.push_back(7) and list1.push_back(8):" << endl;
  30. put_list(list1, "list1");
  31. list1.push_front( 6);
  32. list1.push_front( 5);
  33. cout << "list1.push_front(6) and list1.push_front(5):" << endl;
  34. put_list(list1, "list1");
  35. list1.insert(++list1.begin(), 3, 9);
  36. cout << "list1.insert(list1.begin()+1,3,9):" << endl;
  37. put_list(list1, "list1");
  38. //测试引用类函数
  39. cout << "list1.front()=" << list1.front() << endl;
  40. cout << "list1.back()=" << list1.back() << endl;
  41. list1.pop_front();
  42. list1.pop_back();
  43. cout << "list1.pop_front() and list1.pop_back():" << endl;
  44. put_list(list1, "list1");
  45. list1.erase(++list1.begin());
  46. cout << "list1.erase(++list1.begin()):" << endl;
  47. put_list(list1, "list1");
  48. list2.assign( 8, 1);
  49. cout << "list2.assign(8,1):" << endl;
  50. put_list(list2, "list2");
  51. cout << "list1.max_size(): " << list1.max_size() << endl;
  52. cout << "list1.size(): " << list1.size() << endl;
  53. cout << "list1.empty(): " << list1. empty() << endl;
  54. put_list(list1, "list1");
  55. put_list(list3, "list3");
  56. cout << "list1>list3: " << (list1 > list3) << endl;
  57. cout << "list1<list3: " << (list1 < list3) << endl;
  58. list1.sort();
  59. put_list(list1, "list1");
  60. list1.splice(++list1.begin(), list3);
  61. put_list(list1, "list1");
  62. put_list(list3, "list3");
  63. Sleep( 10000);
  64. }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值