目录

1.list的介绍及使用

1.1list的介绍

1.2list的使用

1.2.1list的构造

1.2.2list literator的使用

1.2.3list capacity

2.list的模拟实现


1.list的介绍及使用

1.1list的介绍

1. list 是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
2. list 的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
3. list forward_list 非常相似:最主要的不同在于 forward_list 是单链表,只能朝前迭代,已让其更简单高效。
4. 与其他的序列式容器相比 (array vector deque) list 通常在任意位置进行插入、移除元素的执行效率更好。
5. 与其他序列式容器相比, list forward_list 最大的缺陷是不支持任意位置的随机访问,比如:要访问 list的第6 个元素,必须从已知的位置 ( 比如头部或者尾部 ) 迭代到该位置,在这段位置上迭代需要线性的时间开销;list 还需要一些额外的空间,以保存每个节点的相关联信息 ( 对于存储类型较小元素的大 list 来说这可能是一个重要的因素。

1.2list的使用

1.2.1list的构造

1.2.2list literator的使用

        list的迭代器是用类进行封装,这样就可以重载*,++,--等运算符,使得list迭代器很好用,它的底层实现后面的代码中会展示。

【注意】
1. beginend为正向迭代器,对迭代器执行++操作,迭代器向后移动
2. rbegin(end)rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动

1.2.3list capacity

 1.2.4list element access

 1.2.5list modifiers

 1.2.6list的迭代器失效

前面说过,此处大家可将迭代器暂时理解成类似于指针, 迭代器失效即迭代器所指向的节点的无效,即该节 点被删除了 。因为 list 的底层结构为带头结点的双向循环链表 ,因此 list 中进行插入时是不会导致 list 的迭代 器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响

2.list的模拟实现

#include<iostream>
using namespace std;

namespace fish
{
    template<class T>
    struct ListNode
    {
        ListNode(const T& val = T()) 
            :_pPre(nullptr),
            _pNext(nullptr),
            _val(val)
        {}
        ListNode<T>* _pPre;
        ListNode<T>* _pNext;
        T _val;
    };

    template<class T, class Ref, class Ptr>

    struct ListIterator
    {
        typedef ListNode<T>* PNode;
        typedef ListIterator<T, Ref, Ptr> Self;

        ListIterator(PNode pNode = nullptr)
            :_pNode(pNode)
        {}
        ListIterator(const Self& l)
            :_pNode(l._pNode)
        {}
        Ref operator*() {
            return _pNode->_val;
        }
        Ptr operator->() {
            return &(_pNode->_val);
        }
        Self& operator++() {
            _pNode = _pNode->_pNext;
            return *this;
        }
        Self operator++(int) {
            Self tmp = _pNode;
            _pNode = _pNode->_pNext;
            return tmp;
        }
        Self& operator--() {
            _pNode = _pNode->_pPre;
            return *this;
        }
        Self& operator--(int) {
            Self tmp = _pNode;
            _pNode = _pNode->_pPre;
            return tmp;
        }
        bool operator!=(const Self& l) {
            return _pNode != l._pNode;
        }
        bool operator==(const Self& l) {
            return _pNode == l._pNode;
        }
        PNode _pNode;
    };

    template<class T>
    class list
    {
        typedef ListNode<T> Node;
        typedef Node* PNode;
    public:
        typedef ListIterator<T, T&, T*> iterator;
        typedef ListIterator<T, const T&, const T*> const_iterator;
    public:
        list(){
            CreateHead();
        }
        list(int n, const T& value = T()) {
            CreateHead();
            while (n > 0) {
                push_back(value);
                n--;
            }
        }
        template <class Iterator>
        list(Iterator first, Iterator last) {
            CreateHead();
            while (first != last) {
                push_back(*first);
                first++;
            }
        }
        list(const list<T>& l) {
            CreateHead();
            iterator curl(l._pHead->_pNext);
            while (curl != l._pHead) {
                push_back(*curl);
                ++curl;
            }
        }
        list<T>& operator=(const list<T> l) {
            CreateHead();
            iterator curl(l._pHead->_pNext);
            while (curl != l._pHead) {
                push_back(*curl);
                ++curl;
            }
            return *this;
        }
        ~list() {
            clear();
            delete _pHead;
            _pHead = nullptr;
        }
        iterator begin() {
            return _pHead->_pNext;
        }
        iterator end() {
            return _pHead;
        }
        const_iterator cbegin() {
            return _pHead->_pNext;
        }
        const_iterator cend() {
            return _pHead;
        }
        size_t size()const {
            PNode p = _pHead->_pNext;
            size_t count = 0;
            while (p != _pHead) {
                count++;
                p = p->_pNext;
            }
            return count;
        }
        bool empty()const {
            return _pHead == _pHead->_pNext;
        }
        T& front(){
            return _pHead->_pNext->_val;
        }
        const T& front()const {
            return _pHead->_pNext->_val;
        }
        T& back() {
            return _pHead->_pPre->_val;
        }
        const T& back()const {
            return _pHead->_pPre->_val;
        }
        void push_back(const T& val) { 
            insert(end(), val); 
        }
        void pop_back() { 
            erase(--end()); 
        }
        void push_front(const T& val) { 
            insert(begin(), val);
        }
        void pop_front() { 
            erase(begin()); 
        }
        iterator insert(iterator pos, const T& val) {
            iterator cur = pos;
            iterator pre = --pos;
            PNode newNode = new Node(val);
            pre._pNode->_pNext = newNode;
            cur._pNode->_pPre = newNode;
            newNode->_pNext = cur._pNode;
            newNode->_pPre = pre._pNode;
            return ++pre;
        }
        iterator erase(iterator pos) {
            iterator pre(pos._pNode->_pPre);
            iterator next(pos._pNode->_pNext);
            delete pos._pNode;
            pos._pNode = nullptr;
            pos = nullptr;
            pre._pNode->_pNext = next._pNode;
            next._pNode->_pPre = pre._pNode;
            return next;
        }
        void clear() {
            iterator cur = begin();
            while (cur != end()) {
                iterator next = cur._pNode->_pNext;
                delete cur._pNode;
                cur._pNode = nullptr;
                cur = next;
            }
            cur = nullptr;
            _pHead->_pNext = _pHead;
            _pHead->_pPre = _pHead;
        }
        void swap(list<T>& l) {
            PNode tmp = _pHead;
            _pHead = l._pHead;
            l._pHead = tmp;
            tmp = nullptr;
        }
    private:
        void CreateHead() {
            _pHead = new Node;
            _pHead->_pNext = _pHead;
            _pHead->_pPre = _pHead;
            _pHead->_val = T();
        }
        PNode _pHead;
    };
};

        源码实现了常用的增删查改操作和比较等,模拟实现有助于进一步理解stl库中底层代码的实现。对于上述代码的使用逻辑,阅读者可参考官方文档进行学习。

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐