侯捷STL课程及源码剖析学习3: 深度探索容器list
一、容器概览

上图为 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的更多相关文章
- 侯捷STL课程及源码剖析学习2: allocator
以STL 的运用角度而言,空间配置器是最不需要介绍的东西,它总是隐藏在一切组件(更具体地说是指容器,container)的背后,默默工作默默付出. 一.分配器测试 测试代码 #include < ...
- 侯捷STL课程及源码剖析学习1
1.C++标准库和STL C++标准库以header files形式呈现: C++标准库的header files不带后缀名(.h),例如#include <vector> 新式C hea ...
- c++ stl源码剖析学习笔记(一)uninitialized_copy()函数
template <class InputIterator, class ForwardIterator>inline ForwardIterator uninitialized_copy ...
- python源码剖析学习记录-01
学习<Python源码剖析-深度探索动态语言核心技术>教程 Python总体架构,运行流程 File Group: 1.Core Modules 内部模块,例如:imp ...
- Spring源码剖析2:Spring IOC容器的加载过程
spring ioc 容器的加载流程 1.目标:熟练使用spring,并分析其源码,了解其中的思想.这篇主要介绍spring ioc 容器的加载 2.前提条件:会使用debug 3.源码分析方法:In ...
- Spring源码剖析3:Spring IOC容器的加载过程
本文转自五月的仓颉 https://www.cnblogs.com/xrq730 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https ...
- c++ stl源码剖析学习笔记(二)iterator
ITERATOR 迭代器 template<class InputIterator,class T> InputIterator find(InputIterator first,Inpu ...
- c++ stl源码剖析学习笔记(三)容器 vector
stl中容器有很多种 最简单的应该算是vector 一个空间连续的数组 他的构造函数有多个 以其中 template<typename T> vector(size_type n,cons ...
- STL源码剖析 学习笔记 MiniSTL
https://github.com/joeyleeeeeee97 目录: 第二章 空间适配器 第三章 迭代器 第四章 序列式容器(vector,list,deque,stack,heap,prior ...
随机推荐
- python的字符串分片
s = "abcdefg" 对于这样一个字符串进行操作.分片格式为: s[i:j:k] 其中,i和j分别代表子串的起始和末尾:-1则代表倒数第一个元素,同理-2代表倒数第二个元素. ...
- 星号三角形 I python
'''星号三角形 I描述读入一个整数N,N是奇数,输出由星号字符组成的等边三角形,要求:第1行1个星号,第2行3个星号,第3行5个星号,依次类推,最后一行共N的星号.输入示例1:3输出示例2: * * ...
- springboot 整合 redis 共享Session-spring-session-data-redis
参考:https://www.cnblogs.com/ityouknow/p/5748830.html 如何使用 1.引入 spring-boot-starter-redis <dependen ...
- 使用STM32CubeMX生成USB_HOST_HID工程
使用开发板为STM32F105开发板. 原本想将具体步骤给写出来.时间有限.直接将STM32F105_USBH_HID.IOC上传files.cnblogs.com/files/libra13179/ ...
- Dubbo 消费者
1. pom <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</art ...
- maven插件之checkstyle
checkstyle的eclipse插件已经在eclipse中有讲过,用于开发者自查用.但是不能保证开发者能按照checkstyle进行改正或者自查,因此就需要对未checkstyle通过的代码不能编 ...
- ABAP-语法检查
1.界面 2.代码 *&---------------------------------------------------------------------* *& Report ...
- SQL Server 用角色(Role)管理数据库权限
当数据库越来越多,连接到数据库的应用程序,服务器,账号越来越多的时候,为了既能达到满足账号操作数据权限需求,又不扩大其操作权限,保证数据库的安全性,有时候需要用角色来参与到权限管理中,通过角色做一个权 ...
- gitlab jenkins 自动构建
工作中有这样一种需求: 每次提交代码之后,都自动执行 单元测试脚本,进行单元测试 jenkins监听项目的某个分支,设置运行脚本,设置一个url作为回调 利用gitlab的钩子,在每次有提交之后,触发 ...
- setting 常用配置
一,保存logging 信息 # 保存log信息的文件名 LOG_LEVEL = "INFO" LOG_STDOUT = True LOG_ENCODING = 'utf-8' # ...