一、容器概览

  上图为 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. python内置函数使用

    print(abs(1)) #绝对值,正数就是自己 ",''])) #计算可迭代对象中是否为真,其中一个为假,就显示为假 print(all('')) # If the iterable i ...

  2. URL中文乱码及特殊字符处理

    一.中文乱码 IE高版本(应该是9以上,不确定),在get方式请求中中文传到后台容易出现乱码问题.解决方法如下: 1.第一种,换成post方式 如果可以得话换成post方式就可以.如果采用表单或者aj ...

  3. iOS 视图间的几种通信方式

    注:此处视图为广义上的视图,不限于View,ViewController之类的都算. 大致分为三种,Notification, delegate, block 例: 假设要在A视图中调起B视图,B视图 ...

  4. 服务发现 - consul 的介绍、部署和使用(转)

    什么是服务发现 相关源码: spring cloud demo 微服务的框架体系中,服务发现是不能不提的一个模块.我相信了解或者熟悉微服务的童鞋应该都知道它的重要性.这里我只是简单的提一下,毕竟这不是 ...

  5. 使用 Python 把多个 MP4 合成一个视频(转)

    这两天群里有个小伙伴有一个需求, 就是把很多个视频文件 合并成一个. 期间也找了各种软件, 如格式工厂, 但是只能一次合成50个文件, 小伙伴有几千个文件需要合成, 太繁琐; 又比如会声会影, 这个剪 ...

  6. deb 和 rpm 后缀文件 区别和安装

    https://blog.csdn.net/u010977122/article/details/52986217 下载一个ATOM 的deb的安装包

  7. 使用__future__实现从python2.7到python3.x的过渡

    参考链接:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386820023 ...

  8. Unity3D脚本学习——运行时类

    AssetBundle 类,继承自Object.AssetBundles让你通过WWW类流式加载额外的资源并在运行时实例化它们.AssetBundles通过BuildPipeline.BuildAss ...

  9. 编程四剑客awk

    awk  'pattern +{action}' file (1)AWK基本语法参数详解 a:单引号 ''是为了和shell命令区分开: b:大括号{}表示一个命令分组: c:pattern 是一个过 ...

  10. MongoDB应用场景

    数据记录如下 /* 1 */ { "_id" : ObjectId("5b56dd19a171d7e9bfb03ac1"), "name" ...