C++的STL简介(四)

目录

1.List

2.list 模拟实现

2.1基本框架

2.2 list_node

2.3 list

2.3.1 默认构造

2.3.2 析构函数

2.3.3 begin()

2.3.4 end()

2.3.5 size()

2.3.6 empty()

2.3.7 insert()

2.3.8 push_back()

2.3.9 push_front()

2.3.10 erase ()

2.3.11 pop_back()

2.3.12 pop_front()

2.4 list_iterator

2.4.1 - >

2.4.2 *

2.4.3 前置++

2.4.4 后置++

2.4.5 前置--

2.4.6 后置 --

2.4.7 !=

2.4.8 list_iterator完整代码

2.5 list_const_iterator

3.整个实现完整代码


1.List

  • List是连续的容器,而vector是非连续的容器,即list将元素存储在连续的存储器中,而vector存储在不连续的存储器中。

  • 向量(vector)中间的插入和删除是非常昂贵的,因为它需要大量的时间来移动所有的元素。链表克服了这个问题,它是使用list容器实现的。

  • List支持双向,并为插入和删除操作提供了一种有效的方法。

  • 在列表中遍历速度很慢,因为列表元素是按顺序访问的,而vector支持随机访问。

2.list 模拟实现

2.1基本框架

list_node来实现每个节点,list_iterator和list_const_iterator是两个不同版本的迭代器一个是普通迭代器一个是const类型的迭代器,名字上也有所区分,list本身就有迭代器的效果,这个是为了模拟这个,重载了一些基本运算符,而list是统一管理节点,具有增删查改等效果

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <cassert>
#include <algorithm> // For std::reverse

using namespace std;

namespace List
{
    // 节点类
    template<class T>
    class list_node
    {
    public:
        T _data;
        list_node<T>* _next;
        list_node<T>* _prev;

        list_node(const T& data = T())
            : _data(data)
            , _next(nullptr)
            , _prev(nullptr)
        {}
    };

//list迭代器
    template<class T>
    struct list_iterator
    {
        typedef list_node<T> Node;
        typedef list_iterator<T> Self;

        Node* _node;

        list_iterator(Node* node)
            : _node(node)
        { }

       
      
    };

//const版本迭代器
    template<class T>
    struct list_const_iterator
    {
        typedef list_node<T> Node;
        typedef list_const_iterator<T> Self;

        Node* _node;

        // 修正: list_const_iterator 的构造函数应该接受 Node* 类型
        list_const_iterator(Node* node)
            : _node(node)
        { }

    
    };
//实现list
    template<class T>
    class list
    {
        typedef list_node<T> Node;
    public:
        typedef list_iterator<T> iterator;
        typedef list_const_iterator<T> const_iterator;

        list()
        {
            _head = new Node(T());
            _head->_next = _head;
            _head->_prev = _head;
            _size = 0;
        }


        ~list()
        {
            while (!empty())
            {
                pop_front();
            }
            delete _head;
        }

    private:
        Node* _head;
        size_t _size;
    };

  
}
2.2 list_node

定义节点

  template<class T>
  class list_node
  {
  public:
      T _data;
      list_node<T>* _next;
      list_node<T>* _prev;

      list_node(const T& data = T())
          : _data(data)
          , _next(nullptr)
          , _prev(nullptr)
      {}
  };
2.3 list

维护节点

//实现list
    template<class T>
    class list
    {
        typedef list_node<T> Node;
    public:
        typedef list_iterator<T> iterator;
        typedef list_const_iterator<T> const_iterator;

        list()
        {
            _head = new Node(T());
            _head->_next = _head;
            _head->_prev = _head;
            _size = 0;
        }


        ~list()
        {
            while (!empty())
            {
                pop_front();
            }
            delete _head;
        }

    private:
        Node* _head;
        size_t _size;
    };
2.3.1 默认构造
//默认构造

 list()
 {
     _head = new Node(T());
     _head->_next = _head;
     _head->_prev = _head;
     _size = 0;
 }

2.3.2 析构函数
//析构函数

 ~list()
 {
     while (!empty())
     {
         pop_front();
     }
     delete _head;
 }
2.3.3 begin()

开始位置的迭代器

//begin()

 iterator begin()
 {
     return iterator(_head->_next);
 }

//const 
 const_iterator begin() const
 {
     return const_iterator(_head->_next);
 }
2.3.4 end()

结尾位置的迭代器


                
评论 33
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

网恋露被录屏她闺蜜看了抢着和我处大象

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值