list源码1(参考STL源码--侯捷):list节点、迭代器、数据结构

list概述

list相对于vector复杂得多,list是一个双链表,对于插入元素、删除元素,list都相对比较简单

list节点

template <class T>
struct __list_node{
    typedef void* void_pointer;
    void_pointer prev; //类型为void*,其实也可以设置为__list_node<T>*
    void_pointer next;
    T data;
};

list迭代器

list迭代器必须有能力指向list的节点,并有能力进行正确的递增、递减、取值、成员存取等操作,同时list是双向链表,迭代器必须具备前移、后移的能力,所以list提供的是Bidirectional Iterators。

list有一个重要性质:插入(insert)操作和接合(splice)操作都不会造成原有的list迭代器失效,甚至list的元素删除(erase)操作也只有“指向被删除元素”的那个迭代器失效,其它迭代器失效不受任何影响。

template<class T,class Ref,class Ptr>
struct __list_iterator{
    typedef __list_iterator<T,T&,T*> iterator;
    typedef __list_iterator<T,Ref,Ptr> self;

    typedef bidirectional_iterator_tag iterator_category;
    typedef T value_type;
    typedef Ptr pointer;
    typedef Ref reference;
    typedef __list_node<T>* link_type;
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;

    link_type node; //迭代器内部需要一个普通指针,指向list的节点

    //constructor
    __list_iterator(link_type x):node(x){}
    __list_iterator(){}
    __list_iterator(const iterator& x):node(x.node){}

    bool operator ==(const self& x)const{return node==x.node;}
    bool operator !=(const self& x)const{return node!=x.node;}

    //以下对迭代器取值,取的是节点的数据值
    reference operator->() const{return &(operator*());}

    //对迭代器累加
    self& operator++(){
        node=(link_type)((*node).next);
        return *this;
    }
    self operator++(int){
        self tmp=*this;
        ++*this;
        return tmp;
    }

    //对迭代器减1
    self& operator--(){
        node=(link_type)((*node).prev);
        return *this;
    }
    self operator--(int){
        self tmp=*this;
        --*this;
        return tmp;
    }
};

list数据结构

list不仅是一个双向链表,而且还是一个环状双向链表,所以它只需要便可以完整表现整个链表:

template <class T,class Alloc=alloc> //缺省使用alloc为适配器
    class list{
    protected:
        typedef __list_node<T> list_node;
    public:
        typedef list_node* link_type;

    protected:
        link_type node; //只要一个指针,便可表示整个环装双向链表
        ......
    };

如果让node(如上)指向刻意置于尾端的一个空间节点,node便能符合STL对于“前闭后开”区间的要求,成为last迭代器,如下图,这样以来,以下几个函数可以很好完成:

iterator begin(){return (link_type)((*node).next);}
iterator end(){return node;}
bool empty() const{return node->next==node;}
size_type size() const{
    size_type result=0;
    distance(begin(),end(),result);
    return result;
}
//取头结点的内容
reference front(){return *begin();}
//取尾节点的内容
reference back(){return *(--end());}

原文地址:https://www.cnblogs.com/ybf-yyj/p/9843391.html

时间: 2024-08-30 14:42:23

list源码1(参考STL源码--侯捷):list节点、迭代器、数据结构的相关文章

vector源码2(参考STL源码--侯捷)

vector源码1(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector的构造和内存管理 vector所采用的数据结构非常简单:线性连续空间,它是由两个迭代器start和finish分别指向配置得来的连续空间中目前已被使用的范围,并以迭代器end_of_storage指向整块连续空间(含备用空间)的尾端: class vector //详细源码可见 { .......... protected: typedef simple_allo

vector源码1(参考STL源码--侯捷)

vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector概述 Vector是动态空间,随着元素的加入,它的内部机制会自行扩充空间纳入新元素,vector的使用效率,关键在于其对大小的控制以及重新配置时的元素迁移效率. Vector定义摘要 template <class T,class Alloc=alloc>//alloc是SGI STL的空间配置器 class vector { public: typedef T

list源码4(参考STL源码--侯捷):transfer、splice、merge、reverse、sort

list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL源码--侯捷):push_front.push_back.erase.pop_front.pop_back.clear.remove.unique list源码4(参考STL源码--侯捷):transfer.splice.merge.reverse.sort transfer list内部提供一个所

list源码2(参考STL源码--侯捷):constructor、push_back、insert

list的push_back.insert的使用如下: #include<bits/stdc++.h> using namespace std; int main() { int i; list<int> l; cout<<l.size()<<endl; //0 l.push_back(1); l.push_back(3); l.push_back(5); l.push_back(7); l.push_back(9); cout<<l.size(

vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效

vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) #include<bits/stdc++.h> using namespace std; int main(){ vector<int> v(3,3); vector<int>::iterator it=v.begin(); cout<<v.size()<<" "<<v.capacity()<<endl;//3

《STL源码剖析》---stl_iterator.h阅读笔记

STL设计的中心思想是将容器(container)和算法(algorithm)分开,迭代器是容器(container)和算法(algorithm)之间的桥梁. 迭代器可以如下定义:提供一种方法,能够依序寻访某个容器内的所有元素,而又无需暴露该容器的内部表达方式. 在阅读代码之前,要先了解一个新概念:Traits编程技法 template <class T> struct MyIter { typedef T value_type //内嵌型别声明 T *ptr; MyIter(T *p = 0

STL&quot;源码&quot;剖析-重点知识总结

STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点略多 :) 1.STL概述 STL提供六大组件,彼此可以组合套用: 容器(Containers):各种数据结构,如:vector.list.deque.set.map.用来存放数据.从实现的角度来看,STL容器是一种class template. 算法(algorithms):各种常用算法,如:sort.search.copy.erase.从实现的角度来看,STL算法

C++ 《STL源码剖析》学习-vector

本文章是笔者学习<STL源码剖析>的学习笔记,记录的是笔者的个人理解,因为个人的水平有限,难免会有理解不当的地方,而且该书出版的时间比较久,难免会有些不一样.如有不当,欢迎指出. vector是c++中经常用到的数据结构,而且在面试时也会有提及,因此了解vector很重要. 一说到vector,我们就很容易想到另外一个与它十分相似的数据结构,关于它们之间显著的差别,我觉得是在于空间运用的灵活性上.数组是静态的,在声明的时候就要指明其具体的空间大小,而vector是动态的,随着元素的增加,它内部

【转载】STL&quot;源码&quot;剖析-重点知识总结

原文:STL"源码"剖析-重点知识总结 STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点略多 :) 1.STL概述 STL提供六大组件,彼此可以组合套用: 容器(Containers):各种数据结构,如:vector.list.deque.set.map.用来存放数据.从实现的角度来看,STL容器是一种class template. 算法(algorithms):各种常用算法,如:sort.se