• C++ STL: list使用及源码剖析


    list使用

    list常用函数及使用(1) 

    1. #include
    2. #include
    3. #include
    4. int main() {
    5. // 创建list
    6. std::list<int> myList = {5, 2, 9, 1, 5, 6};
    7. // 打印list
    8. std::cout << "Original list: ";
    9. for(auto i = myList.begin(); i != myList.end(); ++i) {
    10. std::cout << *i << ' ';
    11. }
    12. std::cout << '\n';
    13. // 检查list是否为空,然后获取大小
    14. if (!myList.empty()) {
    15. std::cout << "List is not empty and has size: " << myList.size() << '\n';
    16. }
    17. // 访问第一个和最后一个元素
    18. std::cout << "First element: " << myList.front() << '\n';
    19. std::cout << "Last element: " << myList.back() << '\n';
    20. // 向list前后插入元素
    21. myList.push_front(0);
    22. myList.push_back(10);
    23. // 删除第一个和最后一个元素
    24. myList.pop_front();
    25. myList.pop_back();
    26. // 在list中插入元素
    27. auto it = std::find(myList.begin(), myList.end(), 5);
    28. if (it != myList.end()) {
    29. myList.insert(it, 4); // 在第一个5之前插入4
    30. }
    31. // 删除一个特定的元素
    32. myList.remove(2); // 删除所有的2
    33. // 对list进行排序
    34. myList.sort();
    35. // 删除所有连续重复的元素
    36. myList.unique();
    37. // 打印修改后的list
    38. std::cout << "Modified list: ";
    39. for(const auto& elem : myList) {
    40. std::cout << elem << ' ';
    41. }
    42. std::cout << '\n';
    43. return 0;
    44. }

    list常用函数及使用(2) 

    1. #include
    2. #include
    3. #include
    4. int main() {
    5. // 初始化两个list
    6. std::list<int> list1 = {1, 2, 3, 4, 5};
    7. std::list<int> list2 = {6, 7, 8, 9, 10};
    8. // 使用splice将list2的元素转移到list1的末尾
    9. list1.splice(list1.end(), list2);
    10. // 使用remove删除所有的'3'
    11. list1.remove(3);
    12. // 使用remove_if删除所有偶数
    13. list1.remove_if([](const int& value) { return value % 2 == 0; });
    14. // 创建第三个list用于merge操作
    15. std::list<int> list3 = {11, 12, 13};
    16. list1.sort(); // 确保merge前list1是排序的
    17. list3.sort(); // 确保merge前list3是排序的
    18. list1.merge(list3);
    19. // 使用reverse反转list1
    20. list1.reverse();
    21. // 使用swap交换list1和list2的元素
    22. list1.swap(list2);
    23. // 使用resize调整list1的大小
    24. list1.resize(3);
    25. // 使用clear清空list2
    26. list2.clear();
    27. // 使用rbegin和rend进行反向迭代
    28. std::cout << "List1 in reverse: ";
    29. for (auto rit = list1.rbegin(); rit != list1.rend(); ++rit) {
    30. std::cout << *rit << " ";
    31. }
    32. std::cout << "\n";
    33. // 使用cbegin和cend进行const迭代
    34. std::cout << "List1: ";
    35. for (auto cit = list1.cbegin(); cit != list1.cend(); ++cit) {
    36. std::cout << *cit << " ";
    37. }
    38. std::cout << "\n";
    39. return 0;
    40. }
    • splice: 将一个list中的元素转移到另一个list中,不进行元素的复制或移动,而是改变节点的链接。
    • remove: 删除list中所有与给定值匹配的元素。
    • remove_if: 根据给定的条件删除元素。
    • merge: 合并两个已排序的list,并清空被合并的list。
    • sort: 对list中的元素进行排序。
    • reverse: 反转list中元素的顺序。
    • swap: 交换两个list的内容。
    • resize: 调整list的大小,可以增加或减少元素数量。
    • clear: 清空list中的所有元素。
    • rbegin, rend: 提供反向迭代器,用于从list的末尾向开始进行遍历。
    • cbegin, cend: 提供常量正向迭代器,用于从list的开始到末尾的遍历,不允许修改元素。
    • crbegin, crend: 提供常量反向迭代器,用于从list的末尾到开始的遍历,不允许修改元素。

    list的数据结构

    STL中list是使用环状双向链表实现的。它的结点结构定义如下:

    1. template <class T>
    2. struct __list_node {
    3. typedef void* void_pointer;
    4. void_pointer next;
    5. void_pointer prev;
    6. T data;
    7. };

    可以看出list节点是一个双向链表,next指向下一个节点,prev指向前一个节点。

    链表最后使用一个指针指向环形链表的空白节点,空白节点指向头节点,这样就形成了一个环了。

    1. template<class T,class Alloc = alloc> //缺省使用alloc为配置器
    2. class list{  
    3. protected :  
    4.    typedef __list_node list_node ;  
    5. public :  
    6.    typedef list_node* link_type ;  
    7. protected :  
    8.    link_type node ; //只要一个指针,便可以表示整个环状双向链表  
    9.   ...
    10. };

    node是指向list节点的一个指针,可以使用这个指针表示整个环状双向链表。

    如果指针node指向置于尾端的一个空白节点,node就能符合stl对于前闭后开区间的要求,这样以下函数便能轻易完成。

    1. iterator begin() { return (link_type)((*node).next); }
    2. iterator end() { return node; }
    3. bool empty() const { return node->next == node; }
    4. size_type size() const
    5. {
    6. size_type result = 0;
    7. distance(begin(), end(), result);//SGI里面的distance函数作用就是遍历链表
    8. return result;
    9. }
    10. reference front() { return *begin(); }
    11. reference back() { return *(--end()); }

    list的迭代器

    list是一个双向链表实现的容器,元素在内存中不需要连续存放。vector需要其元素在内存中连续存放,vector可以使用普通指针作为迭代器。

    因此,list不能使用普通指针作为迭代器,因为它需要特殊的迭代器。

    list提供的迭代器是双向迭代器(Bidirectional Iterators),允许前移和后移操作​。

    vector插入操作可能会导致容器重新分配内存,这会使所有现有迭代器、引用和指针失效。

    list删除操作,只有指向被删除元素的迭代器会失效,其他迭代器仍然有效​​。插入不会使任何的迭代器失效。

    1. template<class T,class Refclass Ptr>
    2. struct _list_iterator{
    3. typedef _list_iterator iterator;
    4. typedef _list_iterator iterator;​
    5. typedef bidirectional_iterator_tag iterator_category;
    6. typedef T value_type;
    7. typedef Ptr pointer;
    8. typedef Ref reference;
    9. typedef _list_node* link_type;
    10. typedef size_t size_type;
    11. typedef ptrdiff_t difference_type;
    12. link_type node;
    13. _list_iterator(link_type x):node(x){}
    14. _list_iterator(){}
    15. _list_iterator(const iterator& x):node(x.node){}
    16. bool operator==(const self& x) const {return node==x.node;}
    17. bool operator!=(const self& x) const {return node!=x.node;}
    18. reference operator*() const {return (*node).data;}
    19. reference operator->() const {return &(operator*());}
    20.    self& operator++(){
    21.   node=(link_type)((*node).next);
    22.   return *this;
    23.   }
    24.    self operator++(int){
    25.   self tmp=*this;
    26.   ++*this;
    27.   return tmp;
    28.   }
    29.    self& operator--(){
    30.   node=(link_type)((*node).prev);
    31.   return *this;
    32.   }
    33.    self operator--(int){
    34.   self tmp=*this;
    35.   --*this;
    36.   return tmp;
    37.   }
    38. }

    list节点的构造和释放

    1. template <class T, class Alloc = alloc>
    2. class list {
    3. public:
    4. //...
    5. // 默认构造函数
    6. list() { empty_initialize(); }
    7. protected:
    8. // 为结点分配内存
    9. link_type get_node() { return list_node_allocator::allocate(); }
    10. // 回收内存
    11. void put_node(link_type p) { list_node_allocator::deallocate(p); }
    12. // 构造node
    13. link_type create_node(const T& x) {
    14. link_type p = get_node();
    15. construct(&p->data, x);
    16. return p;
    17. }
    18. // 销毁node
    19. void destroy_node(link_type p) {
    20. destroy(&p->data);
    21. put_node(p);
    22. }
    23. // 初始化
    24. void empty_initialize() {
    25. node = get_node();
    26. node->next = node;
    27. node->prev = node;
    28. }
    29. // ...
    30. };

    默认构造函数调用empty_initialize()来初始化链表。这个初始化函数设置了一个哨兵节点(或称为头节点),使得链表的nextprev指针都指向自己,表示一个空的链表。

    list操作

    insert:类似双向链表的插入。

    1. terator insert(iterator position, const T& x)
    2. {
    3. link_type tmp = create_node(x); // 产生一个节点
    4. // 调整双向指针,使tmp插入
    5. tmp->next = position.node;
    6. tmp->prev = position.node->prev;
    7. (link_type(position.node->prev))->next = tmp;
    8. position.node->prev = tmp;
    9. return tmp;
    10. }

    erase:类似双向链表的删除。

    1. iterator erase(iterator position){
    2. link_type next_node=link_type(position.node->next);
    3. link_type prev_node=link_type(position.node->prev_nodext);
    4. prev_node->next=next_node;
    5. next_node->prev=prev_node;
    6. destroy_node(position.node);
    7. return iterator(next_node);
    8. }

     push_front(),push_back(),pop_front(), pop_back()在insert和erase的基础上实现。

    参考:

    《C++ STL 源码剖析

    https://www.cnblogs.com/runnyu/p/5992839.html

    https://www.cnblogs.com/LEEYATWAH/p/11707589.html

  • 相关阅读:
    【双指针】快乐数
    Python Opencv实践 - SIFT关键点检测
    基于商用密码技术的铁路行业统计调查系统安全研究
    java: 无法访问org.mybatis.spring.annotation.MapperScan
    3403(3519Dv500)算子精度比对工具标杆数据生成环境搭建指导(Caffe)
    全网最全谷粒商城记录_09、环境-配置docker阿里云镜像加速
    【原生Ajax】全面了解xhr的概念与使用。
    deepstream学习笔记(四):跟踪模块tracker接入与rtsp流异常问题解决
    【图解RabbitMQ-2】图解JMS规范与AMQP协议是什么
    go | 切片的长度和容量
  • 原文地址:https://blog.csdn.net/m0_52043808/article/details/136073324