STL之list简要实现

本文简要介绍了STL中list的数据结构内存模型及其主要接口,并通过对比sort性能,突显了原版list在排序上的优势,对于100万条随机数据,原版list排序只需1.4秒。
摘要由CSDN通过智能技术生成

(1)内存模型及实现接口

 

(2) 源代码及简要注释

sort性能比较低,对100万条随机数据排序要140秒左右,原版list的sort算法仅要1.4秒

#ifndef TINY_LIST

#include <memory>

namespace tiny_stl
{
    //注意:此tiny_list有四种insert版本
    //其中
    //template <typename Input> 
    //iterator insert(const iterator& pos, const Input &first, const Input &last)
    //和
    //iterator insert(const iterator& pos, size_type count, const value_type& value)
    //可能会发生重载冲突,如果value_type = int
    template <typename T>
    struct list_node //将数据封装为结点
    {
        T *data;
        list_node *next;
        list_node *prev;
        
        list_node(const T& value): data(new T(value)), next(NULL), prev(NULL) { }
        list_node(): data(NULL), next(NULL), prev(NULL) { }
        ~list_node()
        {
            delete data;
        }
        T& operator*() const //重载*,便于取数据
        {
            return *data;
        }
    };
    template <typename T>
    class list_iterator //list专属迭代器
    {
    private:
        using value_type = T;
        using self = list_iterator;
    public:
        list_node<value_type> *node; //所指向的结点
    public:
        list_iterator(list_node<value_type> *_node): node(_node) { }
    public:
        self& operator++() //前置自增
        {
            node = node -> next;
            return *this;
        }
        self operator++(int) //后置自增
        {
            self tmp = *this;
            ++*this;
            return tmp;
        }
        self& operator--() //前置自减
        {
            node = node -> prev;
            return *this;
        }
        self operator--(int)//后置自减
        {
            self tmp = *this;
            --*this;
            return tmp;
        }
        value_type& operator*() const
        {
            return *(*node);
        }
        bool operator==(const self &it) const
        {
            return node == it.node;
        }
        bool operator!=(const self &it) const
        {
            return node != it.node;
        }
    };

    template <typename T>
    bool less_cmp(const T &a, const T &b) //默认小于比较运算符
    {
        return a < b;
    }
    template <typename T>
    bool equal_cmp(const T &a, const T &b) //默认等于比较运算符
    {
        return a == b;
    }

    template <typename T, typename U = std::allocator<T> >
    class tiny_list
    {
    public:
        using value_type = T;
        using allocator_type = U;
        using size_type = size_t;
        using node_type = list_node<value_type>;
    private:
        node_type *node; //空结点
        size_type elem_size; //记录大小
        static allocator_type alloc; //内存分配器,虽然没用
        using iterator = list_iterator<value_type>;
    private:
        template <typename Input>
        node_type* insert_aux(node_type *pos, const Input &first, const Input &last)
        //把first到last之间的所有对象插入到pos之前
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值