一、容器概览

  上图为 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. vue父子组件嵌套的时候遇到 - Component template should contain exactly one root element. If you are using v-i

    转自:https://blog.csdn.net/yangyiboshigou/article/details/72084619

  2. curl 超时设置<转>

    PHP cURL 的超时设置有两个 CURLOPT_CONNECTTIMEOUT 和 CURLOPT_TIMEOUT,他们的区别是: CURLOPT_CONNECTTIMEOUT 用来告诉 PHP 在 ...

  3. hdoj 1003 学习思路

    基本解题思路:动态规划,不考虑穷举,分治. 根据网上,状态转移方程是:MaxSum[i] = Max{ MaxSum[i-1] + A[i], A[i]} 翻译公式:到当前位置i 时,最大子序列和为: ...

  4. Pythagorean Triples 707C

    Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theor ...

  5. 4:list 列表

    list:列表.数组.array . list 是有序的,list的定义以 [] 为标识.如:list1 = ['name1', 'name2', 'name3'] 元素可以是任何类型的,如字符串.数 ...

  6. 剑指offer例题——旋转数组的最小数字

    题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转, ...

  7. npm run dev 报错:missing script:dev

    一.问题: 今天在运行vue项目时,在mac终端输入npm run dev,结果报错: 翻译是: npm错误:缺少script:dev npm错误:完整路径见:users/mymac/ .npm/_l ...

  8. [Nginx]Nginx的基本配置与优化1(完整配置示例与虚拟主机配置)

    ---------------------------------------------------------------------------------------- 完整配置示例: [ n ...

  9. Idea使用Maven异常 --- Maven网络代理设置

    在conf/setting.xml和m2/repository/setting.xml中加入: <proxies> <!-- proxy | Specification for on ...

  10. week05 05restful api

    和第一个项目一样 然后去App.js注册一下 但是呢 新闻是写死在 现在主要输调通前端client和后端server 持续获取新闻 至于真假先不考虑 下面我们回到前端NewsPanel 这个reque ...