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节点

  1. template <class T>
  2. struct __list_node{
  3. typedef void* void_pointer;
  4. void_pointer prev; //类型为void*,其实也可以设置为__list_node<T>*
  5. void_pointer next;
  6. T data;
  7. };

list迭代器

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

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

  1. template<class T,class Ref,class Ptr>
  2. struct __list_iterator{
  3. typedef __list_iterator<T,T&,T*> iterator;
  4. typedef __list_iterator<T,Ref,Ptr> self;
  5.  
  6. typedef bidirectional_iterator_tag iterator_category;
  7. typedef T value_type;
  8. typedef Ptr pointer;
  9. typedef Ref reference;
  10. typedef __list_node<T>* link_type;
  11. typedef size_t size_type;
  12. typedef ptrdiff_t difference_type;
  13.  
  14. link_type node; //迭代器内部需要一个普通指针,指向list的节点
  15.  
  16. //constructor
  17. __list_iterator(link_type x):node(x){}
  18. __list_iterator(){}
  19. __list_iterator(const iterator& x):node(x.node){}
  20.  
  21. bool operator ==(const self& x)const{return node==x.node;}
  22. bool operator !=(const self& x)const{return node!=x.node;}
  23.  
  24. //以下对迭代器取值,取的是节点的数据值
  25. reference operator->() const{return &(operator*());}
  26.  
  27. //对迭代器累加
  28. self& operator++(){
  29. node=(link_type)((*node).next);
  30. return *this;
  31. }
  32. self operator++(int){
  33. self tmp=*this;
  34. ++*this;
  35. return tmp;
  36. }
  37.  
  38. //对迭代器减1
  39. self& operator--(){
  40. node=(link_type)((*node).prev);
  41. return *this;
  42. }
  43. self operator--(int){
  44. self tmp=*this;
  45. --*this;
  46. return tmp;
  47. }
  48. };

list数据结构

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

  1. template <class T,class Alloc=alloc> //缺省使用alloc为适配器
  2. class list{
  3. protected:
  4. typedef __list_node<T> list_node;
  5. public:
  6. typedef list_node* link_type;
  7.  
  8. protected:
  9. link_type node; //只要一个指针,便可表示整个环装双向链表
  10. ......
  11. };

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

  1. iterator begin(){return (link_type)((*node).next);}
  2. iterator end(){return node;}
  3. bool empty() const{return node->next==node;}
  4. size_type size() const{
  5. size_type result=;
  6. distance(begin(),end(),result);
  7. return result;
  8. }
  9. //取头结点的内容
  10. reference front(){return *begin();}
  11. //取尾节点的内容
  12. 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. hdu 1277 AC自动机入门(指针版和数组版)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1277 推荐一篇博客(看思路就可以,实现用的是java): https://www.cnblogs.co ...

  2. convert 函数的使用

    先说问题:今天项目中很多模块出现了一个集体性的问题,时间检索时选择同一天 检索不出数据(表中数据确实存在),其实这个问题在做东西的时候领导说过,记忆中我解决了,但是后来写代码可能把这个问题忘记了! 其 ...

  3. ubuntu6.4系统安装JIRA-7.8

    一.系统环境: system version:ubuntu6.4 openjdk version  (java版本) :1.8.0_191  mysql version:14.14 jira vers ...

  4. angularJs, ui-grid 设置默认group, 及排序

  5. JAVAEE——SpringBoot配置篇:配置文件、YAML语法、文件值注入、加载位置与顺序、自动配置原理

    转载 https://www.cnblogs.com/xieyupeng/p/9664104.html @Value获取值和@ConfigurationProperties获取值比较   @Confi ...

  6. Idea+maven+testNG+Selenium+ReportNG自动化框架搭建

    1.Idea创建一个空的Maven项目 创建后默认项目目录如图所示 2.配置pom.xml文件 <?xml version="1.0" encoding="UTF- ...

  7. python 常用标准库

    标准库和第三方库第一手资料: 在线: 官方文档(https://docs.python.org/) 离线:交互式解释器(dir().help()函数),IPython(tab键提示.?.??) 一.  ...

  8. [BeijingWc2008]雷涛的小猫

    --BZOJ1270 Description 雷涛的小猫雷涛同学非常的有爱心,在他的宿舍里,养着一只因为受伤被救助的小猫(当然,这样的行为是违反学生宿舍管理条例的). 在他的照顾下,小猫很快恢复了健康 ...

  9. ubuntu,day 2 ,退出当前用户,创建用户,查找,su,sudo,管道符,grep,alias,mount,tar解压

    本节内容: 1,文件权限的控制,chmod,chown 2,用户的增删和所属组,useradd,userdel 3,用户组的增删,groupadd,groupdel 4,su,sudo的介绍 5,别名 ...

  10. 多线程安全单例模式学习代码 c++11

    // Singleton.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <mutex> #include & ...