VS_list:
1:list在push_back的时候,调用的是insert函数,传递的位置end()。
void push_back(_Ty&& _Val)
{ // insert element at end
_Insert(_Unchecked_end(), _STD forward<_Ty>(_Val));
}
2:list的end()位置是永久不变的,insert函数传递的参数需要传递两个位置,前面是next的位置(push_back里面传递的end()),后面是这个位置的前一个位置。插入的时候就可以将这个数插入在这两个位置之间。
template<class... _Valty>
void _Insert(_Unchecked_const_iterator _Where,
_Valty&&... _Val)
{ // insert element at _Where
_Nodeptr _Pnode = _Where._Mynode();
_Nodeptr _Newnode =
this->_Buynode(_Pnode, this->_Prevnode(_Pnode), //这个函数为获取一个node,并将其上下指针以及值设置好了。
_STD forward<_Valty>(_Val)...);
_Incsize(1);
this->_Prevnode(_Pnode) = _Newnode;
this->_Nextnode(this->_Prevnode(_Newnode)) = _Newnode;
}
3:_Buynode()是用来获取node的,里面获取过程和设置指针调用了_Buynode0(),然后设置了值,一下是两个函数
template<class... _Valty>
_Nodeptr _Buynode(_Nodeptr _Next, _Nodeptr _Prev,
_Valty&&... _Val)
{ // allocate a node and set links and value
_Nodeptr _Pnode = this->_Buynode0(_Next, _Prev); //调用来获取到一个node
_TRY_BEGIN
this->_Getal().construct(
_STD addressof(this->_Myval(_Pnode)),
_STD forward<_Valty>(_Val)...);
_CATCH_ALL
this->_Getal().deallocate(_Pnode, 1);
_RERAISE;
_CATCH_END
return (_Pnode);
}
_Nodeptr _Buynode0(_Nodeptr _Next,
_Nodeptr _Prev)
{ // allocate a node and set links
_Nodeptr _Pnode = this->_Alnod.allocate(1);
if (_Next == _Nodeptr())
{ // point at self
_Next = _Pnode;
_Prev = _Pnode;
}
_TRY_BEGIN
this->_Alnod.construct(
_STD addressof(this->_Nextnode(_Pnode)), _Next); //设置指针
this->_Alnod.construct(
_STD addressof(this->_Prevnode(_Pnode)), _Prev); //设置指针
_CATCH_ALL
this->_Alnod.deallocate(_Pnode, 1);
_RERAISE;
_CATCH_END
return (_Pnode);
}
4:list的插入函数也是类似的,list的数据增加的时候,内存位置不连续,只需要在两个之间插入值,并设置上下指针即可。VS2013里面实现的是list的end()位置恒久不变,从对象一生成就有了这个end()位置,后面的数据只需要在其他地方添加就可以,这样来保证其迭代器指向为 前闭后开的。