list源码1(参考STL源码--侯捷):list节点、迭代器、数据结构

list源码2(参考STL源码--侯捷):constructor、push_back、insert

list源码3(参考STL源码--侯捷):push_front、push_back、erase、pop_front、pop_back、clear、remove、unique

list源码4(参考STL源码--侯捷):transfer、splice、merge、reverse、sort

list的push_back、insert的使用如下:

#include<bits/stdc++.h>
using namespace std; int main() {
int i;
list<int> l; cout<<l.size()<<endl; // l.push_back();
l.push_back();
l.push_back();
l.push_back();
l.push_back();
cout<<l.size()<<endl; // list<int>::iterator it;
for(it=l.begin();it!=l.end();++it){
cout<<*it<<' '; //1 3 5 7 9
}
cout<<endl; it=find(l.begin(),l.end(),);
if(*it==)
l.insert(it,);
for(auto i:l) cout<<i<<' '; //1 3 99 5 7 9
cout<<endl; it=find(l.begin(),l.end(),);
if(*it==)
l.insert(it,);
for(auto i:l) cout<<i<<' '; //1 3 99 5 7 9
cout<<endl; it=find(l.begin(),l.end(),);
l.insert(it,);
for(auto i:l) cout<<i<<' '; //1 3 99 5 7 9 20
cout<<endl;
return ;
}

list缺省使用alloc作为空间适配器,并据此另外定义了一个list_node_allocator,为的是更方便地以节点大小为配置单位:

template <class T,class Alloc=alloc>
class list{
protected:
typedef __list_node<T> list_node;
//专属之空间适配器,每次配置一个节点大小
typedef simple_alloc<list_node,Alloc> list_node_allocator;
...
};

于是list_node_allocator(n)表示配置n个节点空间,以下4个函数,分别来配置、释放、构造、销毁一个节点:

protected:
//配置一个节点并传回
link_type get_node(){return list_node_allocator::allocate();}
//释放一个节点
void put_node(link_type p){list_node_allocator::deallocate(p);}
//产生(配置并构造)一个节点,带有元素值
link_type create_node(const T& x){
linke_type p=get_node();
construct(&p->data,x);//全局函数,构造/析构基本工具
return p;
}
//销毁(析构并释放)一个节点
void destory_node(link_type p){
destory(&p->data);
put_node(p);//全局函数,构造/析构基本工具
}

list提供有许多constructors,其中一个是default constructor,允许我们不指定任何参数做出一个空的list出来:

public:
list(){empty_initialize();} //产生一个空链表
protected:
void empty_initialize(){
node=get_node(); //配置一个节点空间,令node指向它
node->next=node; //令node的头尾指向自己,不设元素值
node->prev=node;
}

当我们以push_back()将新元素插入list尾端时,此函数内部调用insert():

void push_back(const T& x) {insert(end(),x);}

insert()是一个重载函数,有多种形式,其中最简单的一种如下,符合以上所需,首先配置并构造一个节点,然后在尾端进行适当的指针操作,将新节点插入进去:

//函数的目的:在迭代器position所指位置插入一个节点,内容为x
iterator insert(iterator position,const T& x){
link_type temp=create_node(x);//产生一个节点
//调整双向指针,使temp插入进去
temp->next=position.node;
temp->prev=position.node->prev;
(link_type(position.node->prev))->next=temp;
position.node->prev=temp;
return temp;
}

由于list不像vector那样有可能在空间不足时做重新配置,数据移动的操作,所以插入之前的迭代器仍然有效。

list源码2(参考STL源码--侯捷):constructor、push_back、insert的更多相关文章

  1. list源码4(参考STL源码--侯捷):transfer、splice、merge、reverse、sort

    list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...

  2. list源码1(参考STL源码--侯捷):list节点、迭代器、数据结构

    list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...

  3. list源码3(参考STL源码--侯捷):push_front、push_back、erase、pop_front、pop_back、clear、remove、unique

    list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...

  4. vector源码3(参考STL源码--侯捷):pop_back、erase、clear、insert

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷):空间分配.push_back vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 v ...

  5. vector源码2(参考STL源码--侯捷):空间分配、push_back

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...

  6. vector源码1(参考STL源码--侯捷):源码

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...

  7. vector源码(参考STL源码--侯捷):空间分配导致迭代器失效

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...

  8. STL 源码分析 (SGI版本, 侯捷著)

    前言 源码之前,了无秘密 algorithm的重要性 效率的重要性 采用Cygnus C++ 2.91 for windows cygwin-b20.1-full2.exe 下载地址:http://d ...

  9. STL源码阅读-functor与adapter

    为什么要用仿函数 函数指针不灵活,难以与STL其他组件配合使用 Adapter 将一个class的接口转换为另一个class的接口,使原本因接口不兼容而不能合作的classes,可以一起运作 STL中 ...

随机推荐

  1. Python+Selenium学习--下载文件

    场景 webdriver 允许我们设置默认的文件下载路径.也就是说文件会自动下载并且存在设置的那个目录中,下面以firefox及chrome为例 代码 Firefox下载 为了让Firefox浏览器能 ...

  2. 使用es6的then()方法封装jquery的ajax请求

    使用场景: jsp页面中使用jquery的ajax请求比较频繁,以前vue框架的项目用过axios,所以就想着用then()封装一个公共请求的方法,这样每次请求就不用那么麻烦的写一大堆请求参数了. 示 ...

  3. [leetcode]256. Paint House粉刷房子(三色可选)

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

  4. [leetcode]52. N-Queens II N皇后

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  5. 获奖感想和Java学习总结

    获奖感想和Java学习总结 一.获奖感想 能成为小黄衫第二批的成员之一,我感到非常荣幸.我在对老师给予我的鼓励与肯定感到欣喜之余,更多的是感受到了一种鞭策与期望.小黄衫不仅仅是对我的一种奖励,更是激励 ...

  6. kalman滤波(三)---各种滤波的方法汇总+优化的方法

    大神解答 一.前提 最一般的状态估计问题,我们会根据系统是否线性,把它们分为线性/非线性系统.同时,对于噪声,根据它们是否为高斯分布,分为高斯/非高斯噪声系统.现实中最常见的,也是最困难的问题,是非线 ...

  7. POI2015 解题报告

    由于博主没有BZOJ权限号, 是在洛咕做的题~ 完成了13题(虽然有一半难题都是看题解的QAQ)剩下的题咕咕咕~~ Luogu3585 [POI2015]PIE Solution 模拟, 按顺序搜索, ...

  8. php正则提取html图片(img)src地址与任意属性的方法

    <?php /*PHP正则提取图片img标记中的任意属性*/ $str = '<center><img src="/uploads/images/2017020716 ...

  9. HBase总结(十一)hbase Java API 介绍及使用示例

    几个相关类与HBase数据模型之间的对应关系 java类 HBase数据模型 HBaseAdmin 数据库(DataBase) HBaseConfiguration HTable 表(Table) H ...

  10. Codeforces 835C-Star sky

    题目链接:http://codeforces.com/problemset/problem/835/C 题意:天上有很多星星,每个星星有他自己的坐标和初始亮度,然后每个星星的亮度在一秒内会加一如果大于 ...