一. list 實例node
#include<list> //調用系統的list,雙向循環鏈表結構 using namespace std; int main(void) { list<int> mylist; for(int i = 1; i <= 10; i++) { mylist.push_back(i); //接口,末尾增長 } list<int>::iterator it = mylist.begin(); //迭代器, while(it != mylist.end()) { cout<<*it<<"-->"; //打印內部數字
++it; } }
二. 源碼學習函數
struct _Node;學習
typedef struct _Node* _Nodeptr; //指向節點的指針類型spa
struct _Node{ //_Node這個是節點類型指針
_Nodeptr _Prev; //前驅節點code
_Nodeptr _Next; //後繼節點blog
_Ty _Value; //模板數據類型接口
};ci
struct _Acc { //定義_Acc這個類型源碼學習
typedef struct _Node*& _Nodepref; //指向節點類型指針的引用
typedef _Ty& _Vref; //這個數據類型的引用
static _Nodepref _Next(_Nodeptr _P) //靜態方法, 返回值是節點指針的引用 ,參數是指向節點的指針
{
return ((_Nodepref)(*_P)._Next); //:*_P獲得這個節點,()強制類型轉換的優先級沒有.高,因此此時先取_Next,在進行強制類型轉換的工做,返回一個指向節點指針的引用。
}
static _Nodepref _Prev(_Nodeptr _P)
{
return ((_Nodepref)(*_P)._Prev);
}
static _Vref _Value(_Nodeptr _P)
{
return ((_Vref)(*_P)._Value);}
};
public: //如下的類型是_A這個類下面的類型,_A這個類在空間置配器中定義
typedef typename _A::value_type value_type;
typedef typename _A::pointer_type pointer_type;
typedef typename _A::const_pointer_type const_pointer_type;
typedef typename _A::reference_type reference_type;
typedef typename _A::const_reference_type const_reference_type;
typedef typename _A::size_type size_type; //這個類型其實就是size_tprivate:
_Nodeptr _Head; //指向頭結點的指針
三. 構造函數和析構函數
public:
explicit list():_Head(_Buynode()),_Size(0) //explicit顯示調用此構造函數,給頭一個指向,剛開始0個
{}
~list()
{ //釋放空間和空間配置器有關,在現階段先不關心。
erase(begin(), end()); //調用開始,結束函數釋放空間;
_Freenode(_Head); //釋放頭;
_Head = 0, _Size = 0; //都賦空;
}