一、容器概览

  上图为 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. 恺撒密码 I Python实现

    '''恺撒密码 I描述凯撒密码是古罗马凯撒大帝用来对军事情报进行加解密的算法,它采用了替换方法对信息中的每一个英文字符循环替换为字母表序列中该字符后面的第三个字符,即,字母表的对应关系如下:原文:A ...

  2. 分布式 session

    分布式session的实现方式: 一.Session Replication 方式管理 (即session复制) 简介:将一台机器上的Session数据广播复制到集群中其余机器上 使用场景:机器较少, ...

  3. 关于webstorm链接不上SVN的解决办法

    使用WEBSTROM上传代码是很方便的,但是通过它调用SVN时,经常会出现问题,我在使用它调用TortoiseSVN时就出现了一些问题,好在问题已经解决,现在把解决办法分享给大家: 首先,看看,安装时 ...

  4. day01-MySQL介绍

    一.MySQL的介绍 1.1.MySQL介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 W ...

  5. iOS 申请distribution证书, 公钥,私钥

    私钥只有在本机生成CSR文件的时候会产生,公钥会在CSR文件传给apple时,apple产生.

  6. 微服务-dubbo学习

    什么是微服务: 由于业务发展迅速,为了减少代码和功能重复,方便扩展,部署,维护等因素,将系统业务组件化和服务化拆分,拆分为一个个独立的服务,由服务治理系统统一管理,每个微服务为一个进程,之间的通讯方式 ...

  7. MySQL null与not null和null与空值''的区别

    参考连接:https://segmentfault.com/a/1190000009540449 相信很多用了MySQL很久的人,对这两个字段属性的概念还不是很清楚,一般会有以下疑问: 我字段类型是n ...

  8. Oracle重建表空间操作实例

    由于环境维护或者性能测试需要,经常需要对表空间进行重建操作.重建表空间操作主要分3中情况介绍,分别是业务表空间.临时表空间和回滚段表空间的重建. 重建业务表空间 由于业务规划要求,重建后的业务表空间名 ...

  9. Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans.xsd'

    明明项目没错误,但application.xml就报了错误,这是什么问题呢? 问题在于我们找不到org/springframework/beans/spring-beans这个包,也就是我们的spri ...

  10. windows 激活venv问题

    .\ven\Scripts\activate.\ven\Scripts\activate : 无法加载文件 D:\github\ven\Scripts\activate.ps1,因为在此系统上禁止运行 ...