stl源码学习(版本2.91)--list

一,阅读list()构造函数的收获

1,默认构造函数的作用和被调用的时机

struct no{
no(int i){}
//no(){
// std::cout << "s" << std::endl;
//}
long data;
}; struct A{
no n;
}; int main(){
A a;
}

这段代码报错,提示无法构造A类的a对象,编译器会给A类提供默认构造函数,但是A类的默认构造函数去构造它成员no类的n时,发现no类没有构造函数(理由:因为自己定义了no(int)构造函数,所以编译器就不提供no类的默认构造函数了),所以就无法构造n对象,也就无法构造a对象了。

知识点:

  • 如果类没有自己提供构造函数,则编译器会提供一个默认构造函数
  • 当类A里的成员里有类成员b时,当构造A时,就会去找b的构造函数,如果类b有构造函数或者默认构造函数则构造b成功。
1.cpp: In function ‘int main()’:
1.cpp:22:5: error: use of deleted function ‘A::A()’
A a;
^
1.cpp:12:8: note: ‘A::A()’ is implicitly deleted because the default definition would be ill-formed:

2,allocator和定位new的用法

  • allocator:用于开辟内存空间,但是不调用构造函数
  • 定位new:不开辟内存空间,只调用构造函数

stl_list.h源码节选

template <class T>
struct __list_node {
typedef void* void_pointer;
void_pointer next;
void_pointer prev;
T data;
}; template <class T, class Alloc = alloc>
class list {
protected:
typedef __list_node<T> list_node;
typedef simple_alloc<list_node, Alloc> list_node_allocator;
public:
typedef list_node* link_type; protected:
link_type node;//list唯一的成员,是end()函数的返回值 public:
list() { empty_initialize(); }
protected:
void empty_initialize() {
node = get_node();
node->next = node;
node->prev = node;
}
protected:
link_type get_node() { return list_node_allocator::allocate(); } link_type create_node(const T& x) {
link_type p = get_node();
__STL_TRY {
construct(&p->data, x);
}
__STL_UNWIND(put_node(p));
return p;
}
S
iterator insert(iterator position, const T& x) {
link_type tmp = create_node(x);
tmp->next = position.node;
tmp->prev = position.node->prev;
(link_type(position.node->prev))->next = tmp;
position.node->prev = tmp;
return tmp;
}

stl_alloc.h

template<class T, class Alloc>
class simple_alloc { public:
static T *allocate(size_t n)
{ return 0 == n? 0 : (T*) Alloc::allocate(n * sizeof (T)); }
static T *allocate(void)
{ return (T*) Alloc::allocate(sizeof (T)); }

stl_construct.h

template <class T1, class T2>
inline void construct(T1* p, const T2& value) {
new (p) T1(value);
}

从以上的stl list源码可以看出:

  • list的构造函数list(),只开辟了node的内存空间,并没有构造node里的data对象。理由:这个node的哨兵node不是list里保存数据的节点。
  • 但调用insert方法时,会调用create_node,这里面既开辟了节点的内存空间(通过调用get_node();)又调用了节点里data的构造方法(通过调用construct(&p->data, x);),然后在construct里使用了定位new(new (p) T1(value)

    stl源码学习(版本2.91)--list的更多相关文章

    1. 【STL源码学习】STL算法学习之二

      第一章:前言 学习笔记,记录学习STL算法的一些个人所得,在以后想用的时候可以快速拾起. 第二章:明细 copy 函数原型: template <class InputIterator, cla ...

    2. 【STL源码学习】std::list类的类型别名分析

      有了点模板元编程的traits基础,看STL源码清晰多了,以前看源码的时候总被各种各样的typedef给折腾得看不下去, 将<list>头文件的类继承结构简化如下 #include < ...

    3. 【STL源码学习】细品vector

      第一节:vector简介 vector是一种典型的类模板,使用的时候必须进行实例化. vector的数据存储在数组上,支持随机访问迭代器,支持下标操作[]和at操作,支持手动扩容和自动容量增长. ve ...

    4. 【STL源码学习】STL算法学习之一

      第一章:引子 STL包含的算法头文件有三个:<algorithm><numeric><functional>,其中最大最常用的是<algorithm>, ...

    5. STL源码学习----lower_bound和upper_bound算法

      转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...

    6. STL源码学习----lower_bound和upper_bound算法[转]

      STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...

    7. 【STL源码学习】STL算法学习之四

      排序算法是STL算法中相当常用的一个类别,包括部分排序和全部排序算法,依据效率和应用场景进行选择. 明细: sort 函数原型: template <class RandomAccessIter ...

    8. 【STL源码学习】STL算法学习之三

      第一章:前言 数量不多,用到的时候会很爽. 第二章:明细 STL算法中的又一个分类:分割:将已有元素按照既定规则分割成两部分.  is_partitioned 函数原型: template <c ...

    9. [转] STL源码学习----lower_bound和upper_bound算法

      http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...

    随机推荐

    1. OpenCV:图像的裁剪

      import cv2 import matplotlib.pyplot as plt def show(image): plt.imshow(image) plt.axis('off') plt.sh ...

    2. python数据库模块

      安装数据库 [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.3/centos7-amd64 gpgkey=https://yu ...

    3. 并发编程 ~~~ 多进程~~~进程创建的两种方式, 进程pid, 验证进程之间的空间隔离, 进程对象join方法, 进程对象其他属性

      一 进程创建的两种方式 from multiprocessing import Process import time def task(name): print(f'{name} is runnin ...

    4. tomcat的一些优化及报错

      以下为转发来,具体地址为 http://blog.csdn.net/chen3888015/article/details/7432488 环境centos5.7 tomcat6 http://apr ...

    5. Linux—修改ssh远程登录信息

      修改ssh远程登录端口 1.修改ssh服务的配置文件:/etc/ssh/sshd_config ,将 Port 22 改为 Port 3120 保存退出. [root@localhost ~]# vi ...

    6. 使用java代码操作Redis

      1导入pom.xml依赖 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis ...

    7. linux的路由功能实现

      参考URL: https://blog.csdn.net/chengqiuming/article/details/80140768 一,启用Linux的路由转发功能. 二,新建veth pair 三 ...

    8. 前端如何快速定位问题传参 和false

      今天下午在请求接口的时候,出现了一个问题就是  传参问题 接口是请求成功的200,但是修改后返回来的却是500,这就很纳闷怎么会这样了. 经过查找,原来是因为传参问题.将有一个name:0 传递成了n ...

    9. Node.js接口避免重复启动

      众所周知,一个Node接口要是想被调用,得先在命令行中执行如下代码来启动接口 node base.js 但是一旦修改了base.js,就得重新执行这句命令 注:这里的base.js是我的node接口文 ...

    10. GNS3 2.18 + ASA(IOU)

      使用软件及版本 地址:https://www.gns3.com/ gns3: 2.1.18 ASA:asa842-initrd asa842-vmlinuz 一.gns3 vm安装 1.安装 注意:启 ...