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

list的push_back、insert的使用如下:

#include<bits/stdc++.h>
using namespace std;

int main() {
    int i;
    list<int> l;

    cout<<l.size()<<endl; //0

    l.push_back(1);
    l.push_back(3);
    l.push_back(5);
    l.push_back(7);
    l.push_back(9);
    cout<<l.size()<<endl; //5

    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(),5);
    if(*it==5)
        l.insert(it,99);
    for(auto i:l) cout<<i<<‘ ‘; //1 3 99 5 7 9
    cout<<endl;

    it=find(l.begin(),l.end(),55);
    if(*it==55)
        l.insert(it,20);
    for(auto i:l) cout<<i<<‘ ‘; //1 3 99 5 7 9
    cout<<endl;

    it=find(l.begin(),l.end(),55);
    l.insert(it,20);
    for(auto i:l) cout<<i<<‘ ‘; //1 3 99 5 7 9 20
    cout<<endl;
    return 0;
}

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那样有可能在空间不足时做重新配置,数据移动的操作,所以插入之前的迭代器仍然有效。

原文地址:https://www.cnblogs.com/ybf-yyj/p/9883315.html

时间: 2024-07-29 22:45:51

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

vector源码2(参考STL源码--侯捷)

vector源码1(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector的构造和内存管理 vector所采用的数据结构非常简单:线性连续空间,它是由两个迭代器start和finish分别指向配置得来的连续空间中目前已被使用的范围,并以迭代器end_of_storage指向整块连续空间(含备用空间)的尾端: class vector //详细源码可见 { .......... protected: typedef simple_allo

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

vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector概述 Vector是动态空间,随着元素的加入,它的内部机制会自行扩充空间纳入新元素,vector的使用效率,关键在于其对大小的控制以及重新配置时的元素迁移效率. Vector定义摘要 template <class T,class Alloc=alloc>//alloc是SGI STL的空间配置器 class vector { public: typedef T

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

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 transfer list内部提供一个所

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

list概述 list相对于vector复杂得多,list是一个双链表,对于插入元素.删除元素,list都相对比较简单 list节点 template <class T> struct __list_node{ typedef void* void_pointer; void_pointer prev; //类型为void*,其实也可以设置为__list_node<T>* void_pointer next; T data; }; list迭代器 list迭代器必须有能力指向lis

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

vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) #include<bits/stdc++.h> using namespace std; int main(){ vector<int> v(3,3); vector<int>::iterator it=v.begin(); cout<<v.size()<<" "<<v.capacity()<<endl;//3

《STL源码剖析》---stl_iterator.h阅读笔记

STL设计的中心思想是将容器(container)和算法(algorithm)分开,迭代器是容器(container)和算法(algorithm)之间的桥梁. 迭代器可以如下定义:提供一种方法,能够依序寻访某个容器内的所有元素,而又无需暴露该容器的内部表达方式. 在阅读代码之前,要先了解一个新概念:Traits编程技法 template <class T> struct MyIter { typedef T value_type //内嵌型别声明 T *ptr; MyIter(T *p = 0

STL&quot;源码&quot;剖析-重点知识总结

STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点略多 :) 1.STL概述 STL提供六大组件,彼此可以组合套用: 容器(Containers):各种数据结构,如:vector.list.deque.set.map.用来存放数据.从实现的角度来看,STL容器是一种class template. 算法(algorithms):各种常用算法,如:sort.search.copy.erase.从实现的角度来看,STL算法

C++ 《STL源码剖析》学习-vector

本文章是笔者学习<STL源码剖析>的学习笔记,记录的是笔者的个人理解,因为个人的水平有限,难免会有理解不当的地方,而且该书出版的时间比较久,难免会有些不一样.如有不当,欢迎指出. vector是c++中经常用到的数据结构,而且在面试时也会有提及,因此了解vector很重要. 一说到vector,我们就很容易想到另外一个与它十分相似的数据结构,关于它们之间显著的差别,我觉得是在于空间运用的灵活性上.数组是静态的,在声明的时候就要指明其具体的空间大小,而vector是动态的,随着元素的增加,它内部

【转载】STL&quot;源码&quot;剖析-重点知识总结

原文:STL"源码"剖析-重点知识总结 STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点略多 :) 1.STL概述 STL提供六大组件,彼此可以组合套用: 容器(Containers):各种数据结构,如:vector.list.deque.set.map.用来存放数据.从实现的角度来看,STL容器是一种class template. 算法(algorithms):各种常用算法,如:sort.se