• C++ STL进阶与补充(list容器)


    链表的数据采用链式存储,其是一种物理存储单元上非连续的数据结构,数据元素的逻辑顺序是通过链表中的指针链接实现的。

    链表由一系列结点构成。

    节点中一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。

    STL中的链表是一个双向循环链表(有两个指针,一个指前面一个指后面,最后的指针指向最前的地址)。

    链表可以在任意位置插入和删除元素,省去了数据移动的操作。

    链表的缺点:1、容器的遍历速度不如数组,因为地址不是连续的。2、占用的空间比数组大。

    链表的优点:1、采用动态存储分配,不会浪费内存,造成溢出。2、链表的插入和删除十分方便,修改指针即可,不需要移动大量元素。

    链表的迭代器是双向迭代器,仅支持前移和后移。不能跳跃式访问。

    STL中链表和向量是最常用的两个容器。

    1、list构造函数

    List<T> lst;

    List采用模板类实现,默认构造形式

    List(beg,end)

    构造函数将[beg,end]中的元素拷贝到本身

    List(n,elem)

    将n个elem拷贝给本身

    List(const list& lst)

    拷贝构造函数

    1. #include<list>
    2. #include<iostream>
    3. using namespace std;
    4. void printList(const list<int>& l)
    5. {
    6. for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
    7. {
    8. cout << *it << endl;
    9. }
    10. }
    11. void test01()
    12. {
    13. list<int> l1; //默认构造
    14. l1.push_back(10);
    15. l1.push_back(20);
    16. l1.push_back(30);
    17. l1.push_back(40);
    18. printList(l1); //遍历容器
    19. list<int> l2(l1.begin(), l1.end()); //区间方式构造
    20. printList(l2);
    21. //拷贝构造
    22. list<int> l3(l2);
    23. printList(l3);
    24. }
    25. int main()
    26. {
    27. test01();
    28. }

    2、list赋值与交换

    给list容器进行赋值,以及交换list容器。

    Assign(beg,end)

    将[beg,end]区间的数据拷贝过去

    Assign(n,elem)

    将n个elem拷贝给本身

    =

    等号运算符重载

    Swap(list)

    将list与本身的元素进行交换

    1. #include<list>
    2. #include<iostream>
    3. using namespace std;
    4. void printList(const list<int>& l)
    5. {
    6. for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
    7. {
    8. cout << *it << " ";
    9. }
    10. cout << endl;
    11. }
    12. void test01()
    13. {
    14. list<int> l1;
    15. l1.push_back(10);
    16. l1.push_back(20);
    17. l1.push_back(30);
    18. l1.push_back(40);
    19. list<int> l2=l1;
    20. printList(l2);
    21. list<int> l3(l2.begin(),l2.end());
    22. printList(l3);
    23. }
    24. int main()
    25. {
    26. test01();
    27. }

    3、list大小操作

    对list容器的大小进行操作。

    Size()

    返回容器中元素的个数。

    Empty()

    判断容器是否为空。

    Resize(num)

    重新指定容器的长度为num,若容器变长,则以默认值填充新位置;若容器变短,则删除超出长度的元素。

    Resize(num,elem)

    重新指定容器的长度为num,若容器变长,则以elem填充新位置;若容器变短,则删除超出长度的元素。

    1. #include<list>
    2. #include<iostream>
    3. using namespace std;
    4. void printList(const list<int>& l)
    5. {
    6. for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
    7. {
    8. cout << *it << " ";
    9. }
    10. cout << endl;
    11. }
    12. void test01()
    13. {
    14. list<int> l1;
    15. l1.push_back(10);
    16. l1.push_back(20);
    17. l1.push_back(30);
    18. l1.push_back(40);
    19. printList(l1);
    20. if (l1.empty())
    21. {
    22. cout << "l1为空。" << endl;
    23. }
    24. else
    25. {
    26. cout << "l1不为空。" << endl;
    27. cout << "l1的元素个数为:" << l1.size() << endl;
    28. }
    29. //重新指定大小
    30. l1.resize(10);
    31. printList(l1);
    32. }
    33. int main()
    34. {
    35. test01();
    36. }

    4、list插入和删除

    Push_back(elem)

    在容器尾部加入一个元素

    Pop_back()

    删除容器最后一个元素

    Push_front(elem)

    在容器开头插入一个元素

    Pop_front()

    删除容器第一个元素

    Insert(pos,elem)

    在pos位置插入elem元素,返回新数据的位置

    Pos的值是一个迭代器

    Insert(pos,n,elem)

    在pos位置插入n个elem元素,无返回值

    Insert(pos,beg,end)

    在pos位置插入[beg,end]区间内的数据,无返回值

    Clear()

    移除容器中所有数据

    Erase(beg,end)

    删除[beg,end]区间内的数据,返回下一个数据的位置

    Erase(pos)

    删除pos未知的数据,返回下一个数据的位置

    Remove(elem)

    删除容器中所有与elem值相匹配的元素

    1. #include<list>
    2. #include<iostream>
    3. using namespace std;
    4. void printList(const list<int>& l)
    5. {
    6. for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
    7. {
    8. cout << *it << " ";
    9. }
    10. cout << endl;
    11. }
    12. void test01()
    13. {
    14. list<int> l1;
    15. //尾插
    16. l1.push_back(10);
    17. l1.push_back(20);
    18. l1.push_back(30);
    19. //头插
    20. l1.push_front(100);
    21. l1.push_front(200);
    22. l1.push_front(300);
    23. printList(l1); //300 200 100 10 20 30
    24. //尾删
    25. l1.pop_back();
    26. printList(l1); //300 200 100 10 20
    27. //头删
    28. l1.pop_front();
    29. printList(l1); //200 100 10 20
    30. //插入
    31. list<int>::iterator it=l1.begin();
    32. l1.insert(++it, 1000);
    33. printList(l1); //200 1000 100 10 20
    34. //删除
    35. it = l1.begin();
    36. l1.erase(it);
    37. printList(l1); //1000 100 10 20
    38. //移除10
    39. l1.remove(10);
    40. printList(l1); //1000 100 20
    41. //清空
    42. l1.clear();
    43. printList(l1);
    44. }
    45. int main()
    46. {
    47. test01();
    48. }

    5、list数据存取

    Front();

    返回第一个元素

    Back();

    返回最后一个元素

    List不支持[]和at访问里面的数据,不支持随机访问。

    6、list反转和排序

    Reverse()

    反转

    Sort()

    排序,默认从小到大排序。

    所有不支持随机访问迭代器的容器,不可以用标准算法。

    不支持随机访问迭代器的容器,内部会使用成员函数提供相应的算法。

    1. #include<list>
    2. #include<iostream>
    3. using namespace std;
    4. void printList(const list<int>& l)
    5. {
    6. for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
    7. {
    8. cout << *it << " ";
    9. }
    10. cout << endl;
    11. }
    12. bool Compare(int v1, int v2) //通过compare制定排序规则
    13. {
    14. //如果v1>v2,则为降序
    15. return v1 > v2;
    16. }
    17. void test01()
    18. {
    19. list<int> l1;
    20. l1.push_back(10);
    21. l1.push_back(50);
    22. l1.push_back(30);
    23. l1.push_back(20);
    24. cout << "反转前:" << endl;
    25. printList(l1);
    26. l1.reverse();
    27. cout << "反转后:" << endl;
    28. printList(l1);
    29. //排序
    30. l1.sort();
    31. cout << "排序后:" << endl;
    32. printList(l1);
    33. //降序排列
    34. l1.sort(Compare);
    35. printList(l1);
    36. }
    37. int main()
    38. {
    39. test01();
    40. }
  • 相关阅读:
    HTTP学习记录(基于菜鸟教程)
    SECTION概述
    23软考备考已开始,网络工程师知识点速记~(4)
    java并发编程: Semaphore如何快速实现一个限流器?
    表单引擎的自定义控件的概念与设计
    CSS 之 table 表格布局
    PyQt5_股票K线形态查看工具
    SpringBoot整合MybatisPlus多线程下切换数据源的设计方案
    Hybrid Astar 算法剖析和实现(七)
    MLX90640 红外热成像仪测温传感器模块PC端操作教程
  • 原文地址:https://blog.csdn.net/Lao_tan/article/details/125468492