一、容器概览

  上图为 GI STL 2.9的各种容器。图中以内缩方式来表达基层与衍生层的关系。所谓的衍生,并非继承(inheritance)关系,而是内含(containment)关系。例如 heap 内含一个 vector,priority-queue 内含一个 heap,stack  和 queue 都含一个 deque,set/map/multiset/multimap 都内含一个 RB-tree,hast_x都内含一个 hashtable.

二、序列式容器(sequential containers)

所谓序列式容器,其中的元素都可序(ordered),但᳾排序(sorted)。C++  语 言本身提供了一个序列式容器array,STL另外再提供vector,list,deque, stack,queue,priority-queue等等序列式容器。其中stack和queue由于 只是将deque改头换面而成,技术上被归类为一种适配(adapter)。

list 容器

文件依赖结构图

list概览

list 内实现了其迭代器(G2.9)

注意操作符++重载

  虽然iterator也重载operator*(), 但是这里面的*this并没有使用这个重载;例如self tmp = *this, return *this;这两次*this已经被解释为拷贝构造的参数了 ++*this也没有使用重载的operator*();也是已经把*this解释成了operator++()的参数了

简化修改代码

 #include <iostream>
#include <cstdio>
namespace sk
{
template <class T>
struct __list_node {
typedef void* void_pointer;
void_pointer next;
void_pointer prev;
T data;
}; template<class T, class Ref, class Ptr>
struct __list_iterator {
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
typedef __list_iterator<T, Ref, Ptr> self; typedef T value_type;
typedef Ptr pointer;
typedef Ref reference;
typedef __list_node<T>* link_type;
typedef size_t size_type; link_type node; __list_iterator(link_type x) : node(x) { printf("__list_iterator(link_type x) \n"); }
__list_iterator() { printf("__list_iterator() \n"); }
__list_iterator(const iterator& x) : node(x.node) {printf("__list_iterator(const link_type x) \n");} bool operator==(const self& x) const { return node == x.node; }
bool operator!=(const self& x) const { return node != x.node; }
reference operator*() const { printf(" operator*() \n");return (*node).data; } #ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */ self& operator++() {
printf("prev ++ \n");
node = (link_type)((*node).next);
return *this;
}
self operator++(int) {
printf("post ++ \n");
self tmp = *this;
++*this;
return tmp;
}
self& operator--() {
node = (link_type)((*node).prev);
return *this;
}
self operator--(int) {
self tmp = *this;
--*this;
return tmp;
} void operator=(const self &test)
{
printf("operator=\n"); }
}; } typedef sk::__list_iterator<int,int&,int*>
list_iterator_int;
list_iterator_int
testassign(list_iterator_int t)
{
return t;
} int main()
{
sk::__list_node<int> node1;
sk::__list_node<int> node2;
sk::__list_node<int> node3;
sk::__list_node<int> node4;
sk::__list_node<int> node5;
node1.next = &node2;
node2.next = &node3;
node3.next = &node4;
node4.next = &node5; node2.prev = &node1;
node3.prev = &node2;
node4.prev = &node3;
node5.prev = &node4; sk::__list_iterator<int,int&,int*> iter1;
iter1.node = &node2;
printf("-------------------iter1++----------------------\n");
iter1++;
printf("-------------------++iter1----------------------\n");
++iter1;
printf("\n");
printf("-------------------1----------------------\n");
list_iterator_int iter_test;
printf("-------------------2----------------------\n");
iter_test = iter1;
printf("-------------------3----------------------\n");
list_iterator_int iter2 = iter1;
printf("-------------------4----------------------\n");
iter_test = testassign(iter1);
printf("-------------------5----------------------\n");
testassign(iter1);
printf("------------------------------------------\n"); return ;
}

test

测试结果

  • 传参的过程中,要调用一次复制构造函数:   iter1入栈时会调用复制构造函数创建一个临时对象,与函数内的局部变量具有相同的作用域.
  • 函数返回值时,也会构造一个临时对象;调用重载赋值操作符赋值给iter_test.

内容参考整理:侯捷STL课程

侯捷STL课程及源码剖析学习3: 深度探索容器list的更多相关文章

  1. 侯捷STL课程及源码剖析学习2: allocator

    以STL 的运用角度而言,空间配置器是最不需要介绍的东西,它总是隐藏在一切组件(更具体地说是指容器,container)的背后,默默工作默默付出. 一.分配器测试 测试代码 #include < ...

  2. 侯捷STL课程及源码剖析学习1

    1.C++标准库和STL C++标准库以header files形式呈现: C++标准库的header files不带后缀名(.h),例如#include <vector> 新式C hea ...

  3. c++ stl源码剖析学习笔记(一)uninitialized_copy()函数

    template <class InputIterator, class ForwardIterator>inline ForwardIterator uninitialized_copy ...

  4. python源码剖析学习记录-01

    学习<Python源码剖析-深度探索动态语言核心技术>教程         Python总体架构,运行流程   File Group: 1.Core Modules 内部模块,例如:imp ...

  5. Spring源码剖析2:Spring IOC容器的加载过程

    spring ioc 容器的加载流程 1.目标:熟练使用spring,并分析其源码,了解其中的思想.这篇主要介绍spring ioc 容器的加载 2.前提条件:会使用debug 3.源码分析方法:In ...

  6. Spring源码剖析3:Spring IOC容器的加载过程

    本文转自五月的仓颉 https://www.cnblogs.com/xrq730 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https ...

  7. c++ stl源码剖析学习笔记(二)iterator

    ITERATOR 迭代器 template<class InputIterator,class T> InputIterator find(InputIterator first,Inpu ...

  8. c++ stl源码剖析学习笔记(三)容器 vector

    stl中容器有很多种 最简单的应该算是vector 一个空间连续的数组 他的构造函数有多个 以其中 template<typename T> vector(size_type n,cons ...

  9. STL源码剖析 学习笔记 MiniSTL

    https://github.com/joeyleeeeeee97 目录: 第二章 空间适配器 第三章 迭代器 第四章 序列式容器(vector,list,deque,stack,heap,prior ...

随机推荐

  1. Android 组合控件

    前言 自定义组合控件就是多个控件组合起来成为一个新的控件,主要用来解决多次重复的使用同一类型的布局.比如我们应用的顶部的标题栏,还有弹出的固定样式的dialog,这些都是常用的,所以把他们所需要的控件 ...

  2. 对象 Object

    在js 中创建最简单的对象,然后给它添加属性或者方法 示例如下: var obj = new Object(); //或者 var obj = {}; obj.name = '张三'; obj.fun ...

  3. day24-抽象类与接口类

    接口类 1.继承有两种用途:一:继承基类的方法,并且做出自己的改变或者扩展(代码重用) 二:声明某个子类兼容于某基类,定义一个接口类,接口类中定义了一些接口名(就是函数名)且并未实现接口的功能,子类继 ...

  4. js弹出div层,弹出层页面底部出现UL出现一条线问题

    整个弹出div层,列表满一页时:底部会出现一条横线 原因:ul固定写在页面中了 解决方法: 将ul代码与li列表一样写在js中,如下 var newhtml = '<ul class=" ...

  5. 通过SSH克隆远程仓库(GitLab)到本地

    由于不是任何用户都能从远程仓库克隆到本地的,也是需要鉴别的,因此本地需要用git bash 创建一个公钥,而远程仓库也要把这个公钥保存下来,进而本地才可以从远程download.主要步骤如下: 1.首 ...

  6. 解决 'Could not convert variant of type (NULL) into type (String)

    写存储过程中有不允许为空的字段,在客户端转化取数时显示 Could not convert variant of type (NULL) into type (String) 可以在存储过程中使用is ...

  7. Lazarus 0.9.26——UTF8编码副作用

    Lazarus 0.9.26中,涉及范围最广的的改变就是所有的的String默认都采用UTF8编码,IDE终于有了完全的UTF8支持,以前在源码编辑器中“吃掉”半个汉字的情况不再出现.对于Linux下 ...

  8. C++复习:异常

    异常处理机制专题 前言 1)异常是一种程序控制机制,与函数机制独立和互补     函数是一种以栈结构展开的上下函数衔接的程序控制系统,异常是另一种控制结构,它依附于栈结构,却可以同时设置多个异常类型作 ...

  9. 2:if 语句

    if 语句 语法形式: 第一种,只有两个分支: if 表达式: something else: something 第二种,有多个分支: if 表达式1: do something 1 elif 表达 ...

  10. Unable to locate Spring NamespaceHandler for XML schema namespace

    1. 问题 本文将讨论Spring中最常见的配置问题 —— Spring的一个命名空间的名称空间处理程序没有找到. 大多数情况下,是由于一个特定的Spring的jar没有配置在classpath下,让 ...