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

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=;
distance(begin(),end(),result);
return result;
}
//取头结点的内容
reference front(){return *begin();}
//取尾节点的内容
reference back(){return *(--end());}

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

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

    list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...

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

    list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...

  3. list源码3(参考STL源码--侯捷):push_front、push_back、erase、pop_front、pop_back、clear、remove、unique

    list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...

  4. vector源码3(参考STL源码--侯捷):pop_back、erase、clear、insert

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷):空间分配.push_back vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 v ...

  5. vector源码2(参考STL源码--侯捷):空间分配、push_back

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

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

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

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

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

  8. STL 源码分析 (SGI版本, 侯捷著)

    前言 源码之前,了无秘密 algorithm的重要性 效率的重要性 采用Cygnus C++ 2.91 for windows cygwin-b20.1-full2.exe 下载地址:http://d ...

  9. STL源码阅读-functor与adapter

    为什么要用仿函数 函数指针不灵活,难以与STL其他组件配合使用 Adapter 将一个class的接口转换为另一个class的接口,使原本因接口不兼容而不能合作的classes,可以一起运作 STL中 ...

随机推荐

  1. idea导入eclipse项目的配置

    idea导入eclipse项目需要的配置 1.配置jdk,这两处选择一样就可以,也可以根据自己需求选择,上边的比下边的版本高就行 2.这里会默认和配置jdk一样 3.添加lib依赖,选择到项目的lib ...

  2. [剑指Offer]5-替换空格

    链接 https://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e?tpId=13&tqId=11210&tPa ...

  3. python中sys.path--学习

    本着下定义开头吧:python中import某个A模块时,首先会从python的内置模块中查找是否含义该模块的定义若未查询到会从sys.path对应的模块路径查询是否含有对应模块的定义,如果搜索完成依 ...

  4. 解决IE浏览器缓存导致AJAX请求数据异常

    IE10浏览器会把AJAX请求的数据都缓存下来,然后每次想去刷新数据时发现数据都是一样的,于是导致数据显示异常. 解决方法: 在页面<head>标签里,加上以下声明: <!-- 解决 ...

  5. 20165315 2017-2018-2《Java程序设计》课程总结

    20165315 2017-2018-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:我期望的师生关系 预备作业2:C语言基础调查和java学习展望 预备作业3:Linux安 ...

  6. linux下apt安装mysql导致mysql.user table is damaged

    笔者在ubuntu下用 apt install mysql-server类似的命令安装mysql, 安装了最新版的mysql5.7,覆盖了操作系统内置的数据库mysql系统库. 最初启动mysql出错 ...

  7. Windows7 VS2015 下编译 PythonQt3.2

    本文在使用vs2015编译python3.6.7源代码后,编译的PythonQt3.2.如果使用python二进制文件进行安装,注意python的路径即可 本机环境: 1.win7 64 旗舰版 2. ...

  8. 初始Spring mvc

    转自:http://elf8848.iteye.com/blog/875830很棒的一篇博客,想了解SpringMvc的入门选手可以去看看. 一,核心类与接口: DispatcherServlet - ...

  9. nc6 用业务插件注册来跑按钮事件

    在实际开发中,有些需求是要求系统单据,编辑或者触发其他按钮来回写其他模块单据 这时候就能用业务插件方式来触发其他模块的按钮事件,而不用去模块找对应的按钮编辑事件类 package hz.bs.hzct ...

  10. eclipse里maven项目An error occurred while filtering resources解决办法

    在使用eclipse构建maven项目时,突然出现错误提示:An error occurred while filtering resources,在项目中到处都找不到哪里有问题,最后在国外网站找到解 ...