vector的源码真是太长了,今天用了一个下午和一个晚上看和注释了前面的一千行左右
p.s.博客园的代码高亮真是太垃圾, 如果想要阅读带注释的源码,推荐粘贴到VS2015里,然后按ctrl+z取消自动格式化,用我格式化好的样子,并在最前面#include <vector>和using namespace std,这样就能带高亮的看我加了注释的代码了
花了不短的时间弄明白了vector奇怪的继承方式,用我自己创造的一种标记记法记了下来
_Vector_val<_Val_types>: _Container_base
{
'namely':
_Val_types = _Vec_base_types<_Ty, _Alloc>::_Val_types
'use':
_Val_types::pointer
}
_Vec_base_types<_Ty, _Alloc>
{
'namely':
_Ty = 'value'
_Alloc = 'allocator'
'define':
_Val_types <= _Simple_types, _Vec_iter_types
}
_Vector_alloc<_Alloc_types>
{
'namely':
_Alloc_types = _Vec_base_types<_Ty, _Alloc>
'alias':
_Val_types = _Alloc_types::_Val_types
'use':
_Vetor_val<_Val_types>
}
vector: _Vector_alloc<_Vec_base_types<_Ty, _Alloc>>
{
}
简单说,vector继承自_Vector_alloc<T>,_Vector_alloc使用模板参数T内的类型在内部构造_Vector_val<T'>的实例,_Vector_val内部使用模板参数T'内的类型,构造出需要被封装的3个指针:first,last和end
_Vec_base_types和_Val_types都是一个类型包,里面包了各种抽取出来的类型,其中_Val_types由_Vec_base_types定义
下面是原代码注释
template<class _Value_type>
struct _Simple_types
/*
简单类的类型别名
*/
{
typedef _Value_type value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
};
template<class _Ty,
class _Alloc0>
struct _Vec_base_types
/*
这个类获取了和vector本身相关的各种类型
*/
{
typedef _Alloc0 _Alloc;
/*
_Alloc0就是vector的allocator类型参数,默认是allocator<_Ty>
*/
typedef _Vec_base_types<_Ty, _Alloc> _Myt;
/*
这个类本体的类型别名
*/
typedef _Wrap_alloc<_Alloc> _Alty0;
/*
用来抽取这个allocator的traits的包装
*/
typedef typename _Alty0::template rebind<_Ty>::other _Alty;
/*
rebind<_Ty>::other 返回 allocator<_Ty>
如果有奇怪的vector的allocator<T>的T不是元素的T,这个就有用了
*/
typedef typename _If<_Is_simple_alloc<_Alty>::value,
/*
获取value_type的traits的包装
如果_Alty是simple_alloc————这个后面写清楚
*/
_Simple_types<typename _Alty::value_type>,
/*
就使用前面的_Simple_types把相关类型抽取出来
*/
_Vec_iter_types<typename _Alty::value_type,
typename _Alty::size_type,
typename _Alty::difference_type,
typename _Alty::pointer,
typename _Alty::const_pointer,
typename _Alty::reference,
typename _Alty::const_reference>
/*
否则手动指定_Alty中抽取的类型作为_Val_types的类型包装
*/
>::type _Val_types;
/*
为什么需要这个判断呢,因为C++允许用户把一个类抽象的完全不像样子,包括
修改各种与之相关的类型的定义,比如指针,引用
重载运算符使之有不同的行为,比如取地址运算符
这时候如果用这样改的乱七八糟的类接入stl,就需要用户自己提供一个allocator<T>特化
为了在stl容器内部的实现中仍然使用原生的T*,T&等类型,就需要这种trick,恢复一个类本来的面目
*/
};
template<class _Alty>
struct _Is_simple_alloc
/*
当满足如下情况时,一个allocator<Ty>一个simple allocator
*/
: _Cat_base<is_same<typename _Alty::size_type, size_t>::value
/*
_Cat_base把一个基本类型的值v包装成可以继承的class,内含一个value,值是这个v
*/
&& is_same<typename _Alty::difference_type,
ptrdiff_t
>::value
/*
它的difference_type是ptrdiff_t即int
*/
&& is_same<typename _Alty::pointer,
typename _Alty::value_type*
>::value
/*
它的指针是value_type*
*/
&& is_same<typename _Alty::const_pointer,
const typename _Alty::value_type*
>::value
/*
它的常量指针是const value_type*
*/
&& is_same<typename _Alty::reference,
typename _Alty::value_type&
>::value
/*
它的引用是value_type&
*/
&& is_same<typename _Alty::const_reference,
const typename _Alty::value_type&
>::value
/*
它的常量引用是const value_type&
*/
>
{
//这里继承了基类中的const static bool value,值是true或者false,表示一个类是不是simple allocator
};
template<class _Val_types>
/*
_Val_types,前面与vector成员类型T相关联的其他类型
*/
class _Vector_val: public _Container_base
/*
vector的私有成员的包装,_Myfirst, _Mylast, _Myend
*/
{
public:
typedef _Vector_val<_Val_types> _Myt;
/*
类自己的别名
*/
typedef typename _Val_types::value_type value_type;
typedef typename _Val_types::size_type size_type;
typedef typename _Val_types::difference_type difference_type;
typedef typename _Val_types::pointer pointer;
typedef typename _Val_types::const_pointer const_pointer;
typedef typename _Val_types::reference reference;
typedef typename _Val_types::const_reference const_reference;
/*
给这些相关类型又取了个别名
*/
typedef _Vector_iterator<_Myt> iterator;
typedef _Vector_const_iterator<_Myt> const_iterator;
/*
迭代器之后讲
*/
_Vector_val()
/*
简单的初始化
*/
{
_Myfirst = pointer();
_Mylast = pointer();
_Myend = pointer();
}
pointer _Myfirst;
pointer _Mylast;
pointer _Myend;
};
template<class _Alloc_types>
/*
_Alloc_types = _Vector_base_type
*/
class _Vector_alloc
/*
放vector的allocator的地方
*/
{
public:
typedef _Vector_alloc<_Alloc_types> _Myt;
typedef typename _Alloc_types::_Alloc _Alloc;
typedef typename _Alloc_types::_Alty _Alty;
typedef typename _Alloc_types::_Val_types _Val_types;
typedef typename _Val_types::value_type value_type;
typedef typename _Val_types::size_type size_type;
typedef typename _Val_types::difference_type difference_type;
typedef typename _Val_types::pointer pointer;
typedef typename _Val_types::const_pointer const_pointer;
typedef typename _Val_types::reference reference;
typedef typename _Val_types::const_reference const_reference;
typedef _Vector_iterator<_Vector_val<_Val_types>> iterator;
typedef _Vector_const_iterator<_Vector_val<_Val_types>> const_iterator;
private:
_Compressed_pair<_Alty, _Vector_val<_Val_types>> _Mypair;
/*
简单说,如果第一个模板参数是一个无数据成员的类
那_Compressed_pair通过继承自第一个模板参数
来避免第一个模板参数的实例占用1字节的空间
*/
public:
#if _ITERATOR_DEBUG_LEVEL == 0
/*
release模式下的成员函数,去掉了迭代器范围检查
_Vector_val的_Container_base基类是空类
*/
_Vector_alloc(const _Alloc& _Al = _Alloc())
: _Mypair(_One_then_variadic_args_t(), _Al)
{ // construct allocator from _Al
}
_Vector_alloc(_Alloc&& _Al)
: _Mypair(_One_then_variadic_args_t(), _STD move(_Al))
{ // construct allocator from _Al
}
void _Copy_alloc(const _Alty& _Al)
{ // replace old allocator
_Pocca(_Getal(), _Al);
}
void _Move_alloc(_Alty& _Al)
{ // replace old allocator
_Pocma(_Getal(), _Al);
}
void _Swap_alloc(_Myt& _Right)
{ // swap allocators
_Pocs(_Getal(), _Right._Getal());
}
#else /* _ITERATOR_DEBUG_LEVEL == 0 */
/*
debug模式下,_Vector_val的_Container_base基类里有一个proxy成员
是一个保存着这个vector所有迭代器的链表
release模式下_Container_base是空类
*/
_Vector_alloc(const _Alloc& _Al = _Alloc())
: _Mypair(_One_then_variadic_args_t(), _Al)
/*
构造allocator,使用_Al显式构造_Mypair的第一个成员
*/
{
_Alloc_proxy();
/*
构造proxy链表
*/
}
_Vector_alloc(_Alloc&& _Al)
: _Mypair(_One_then_variadic_args_t(), _STD move(_Al))
{
_Alloc_proxy();
}
/*
移动构造
*/
~_Vector_alloc() _NOEXCEPT
{ // destroy proxy
_Free_proxy();
}
/*
析构,删除proxy
*/
void _Copy_alloc(const _Alty& _Al)
/*
拷贝allocator
*/
{
_Free_proxy();
_Pocca(_Getal(), _Al);
/*
按需把_Al拷贝过来
pocca = propagate pn copy assignment
*/
_Alloc_proxy();
}
void _Move_alloc(_Alty& _Al)
{ // replace old allocator
_Free_proxy();
_Pocma(_Getal(), _Al);
/*
按需把_Al移动过去
pocma = propagate on move assignment
*/
_Alloc_proxy();
}
void _Swap_alloc(_Myt& _Right)
{ // swap allocators
_Pocs(_Getal(), _Right._Getal());
/*
按需交换两个allocator
pocs = propagate on container swap
*/
_Swap_adl(_Myproxy(), _Right._Myproxy());
}
void _Alloc_proxy()
/*
分配一个proxy
*/
{
typename _Alty::template rebind<_Container_proxy>::other
_Alproxy(_Getal());
/*先构造proxy的allocator*/
_Myproxy() = _Alproxy.allocate(1);
_Alproxy.construct(_Myproxy(), _Container_proxy());
/*构造proxy的头节点*/
_Myproxy()->_Mycont = &_Get_data();
/*
头节点的容器指针指向这个类的_Vector_val,也就是实际存储着三个指针的那个东西的地址
*/
}
void _Free_proxy()
/*
释放所有的proxy
*/
{
typename _Alty::template rebind<_Container_proxy>::other
_Alproxy(_Getal());
_Orphan_all();
_Alproxy.destroy(_Myproxy());
_Alproxy.deallocate(_Myproxy(), 1);
_Myproxy() = 0;
}
_Iterator_base12 **_Getpfirst() const
/*
获取迭代器链表的头的指针
*/
{
return (_Get_data()._Getpfirst());
}
_Container_proxy * & _Myproxy() _NOEXCEPT
/*
返回迭代器链表头的引用
*/
{
return (_Get_data()._Myproxy);
}
_Container_proxy * const & _Myproxy() const _NOEXCEPT
/*
返回常量引用
*/
{
return (_Get_data()._Myproxy);
}
#endif /* _ITERATOR_DEBUG_LEVEL == 0 */
void _Orphan_all()
{
_Get_data()._Orphan_all();
}
void _Swap_all(_Myt& _Right)
{
_Get_data()._Swap_all(_Right._Get_data());
}
_Alty& _Getal() _NOEXCEPT
{
return (_Mypair._Get_first());
}
const _Alty& _Getal() const _NOEXCEPT
{
return (_Mypair._Get_first());
}
/*
返回allocator<_Ty>还记得前面有可能传入的allocator<T>的T不是value_type吗
这里返回的一定是allocator<_Ty>
*/
_Vector_val<_Val_types>& _Get_data() _NOEXCEPT
{ // return reference to _Vector_val
return (_Mypair._Get_second());
}
const _Vector_val<_Val_types>& _Get_data() const _NOEXCEPT
{ // return const reference to _Vector_val
return (_Mypair._Get_second());
}
/*
获取三个指针及其本体
*/
pointer& _Myfirst() _NOEXCEPT
{ // return reference to _Myfirst
return (_Get_data()._Myfirst);
}
const pointer& _Myfirst() const _NOEXCEPT
{ // return const reference to _Myfirst
return (_Get_data()._Myfirst);
}
pointer& _Mylast() _NOEXCEPT
{ // return reference to _Mylast
return (_Get_data()._Mylast);
}
const pointer& _Mylast() const _NOEXCEPT
{ // return const reference to _Mylast
return (_Get_data()._Mylast);
}
pointer& _Myend() _NOEXCEPT
{ // return reference to _Myend
return (_Get_data()._Myend);
}
const pointer& _Myend() const _NOEXCEPT
{ // return const reference to _Myend
return (_Get_data()._Myend);
}
/*
获取三个指向位置的指针的引用
*/
};
template<class _Ty, class _Alloc = allocator<_Ty>>
class vector: public _Vector_alloc<_Vec_base_types<_Ty, _Alloc>>
/*
vector本体,继承自_Vector_alloc的包装
*/
{
public:
typedef vector<_Ty, _Alloc> _Myt;
typedef _Vector_alloc<_Vec_base_types<_Ty, _Alloc>> _Mybase;
typedef typename _Mybase::_Alty _Alty;
/*
方便直接访问的类型别名定义
*/
typedef _Alloc allocator_type;
typedef typename _Mybase::value_type value_type;
typedef typename _Mybase::size_type size_type;
typedef typename _Mybase::difference_type difference_type;
typedef typename _Mybase::pointer pointer;
typedef typename _Mybase::const_pointer const_pointer;
typedef typename _Mybase::reference reference;
typedef typename _Mybase::const_reference const_reference;
/*
接口要求的类型别名定义
*/
#define _VICONT(it) it._Getcont()
#define _VIPTR(it) (it)._Ptr
typedef typename _Mybase::iterator iterator;
typedef typename _Mybase::const_iterator const_iterator;
typedef _STD reverse_iterator<iterator> reverse_iterator;
typedef _STD reverse_iterator<const_iterator> const_reverse_iterator;
/*
迭代器定义
*/
vector() _NOEXCEPT
: _Mybase()
{
}
/*
构造空迭代器
*/
explicit vector(const _Alloc& _Al) _NOEXCEPT
: _Mybase(_Al)
{
}
/*
用已有的allocator构造
*/
explicit vector(size_type _Count)
: _Mybase()
{
if (_Buy(_Count))
/*
为false的情况只有_Count == 0的情况
*/
{
try
{
_Uninitialized_default_fill_n(this->_Myfirst(), _Count, this->_Getal());
/*
使用默认构造填充刚分配的内存
*/
this->_Mylast() += _Count;
}
catch(...)
{
_Tidy();
throw;
}
}
}
vector(size_type _Count, const value_type& _Val)
: _Mybase()
{
_Construct_n(_Count, _STD addressof(_Val));
}
vector(size_type _Count, const value_type& _Val, const _Alloc& _Al)
: _Mybase(_Al)
{
_Construct_n(_Count, _STD addressof(_Val));
}
/*
构造_Count个_Val的副本
*/
vector(const _Myt& _Right)
: _Mybase(_Right._Getal().select_on_container_copy_construction())
/*
select_on_container_copy_construction
这个函数返回一个allocator副本,如果有;如果没有,那就返回allocator引用
*/
{
if (_Buy(_Right.size()))
{
try
{
this->_Mylast() = _Ucopy(_Right.begin(), _Right.end(), this->_Myfirst());
/*
从_Right的序列拷贝构造到新的内存
*/
}
catch (...)
{
_Tidy();
throw;
}
}
}
vector(const _Myt& _Right, const _Alloc& _Al)
: _Mybase(_Al)
{
if (_Buy(_Right.size()))
{
try
{
this->_Mylast() = _Ucopy(_Right.begin(), _Right.end(), this->_Myfirst());
}
catch (...)
{
_Tidy();
throw;
}
}
}
/*
没啥难理解的
*/
template<class _Iter, class = typename enable_if<_Is_iterator<_Iter>::value, void>::type>
vector(_Iter _First, _Iter _Last)
: _Mybase()
/*
利用SFINAE,当_Iter是迭代器类型的时候使用这个函数
*/
{
_Construct(_First, _Last);
/*
直接构造
*/
}
template<class _Iter, class = typename enable_if<_Is_iterator<_Iter>::value, void>::type>
vector(_Iter _First, _Iter _Last, const _Alloc& _Al)
: _Mybase(_Al)
{
_Construct(_First, _Last);
}
/*
同上
*/
template<class _Iter>
void _Construct(_Iter _First, _Iter _Last)
{
_Construct(_First, _Last, _Iter_cat(_First));
/*
_Iter_cat返回iterator的category
*/
}
/*
拷贝构造的外包装
*/
template<class _Iter>
void _Construct(_Iter _First, _Iter _Last, input_iterator_tag)
{
try
{
for (; _First != _Last; ++_First)
{
emplace_back(*_First);
/*
输入迭代器,只能一次一次读取,并在尾部构造
*/
}
}
catch(...)
{
_Tidy();
throw;
}
}
template<class _Iter>
void _Construct(_Iter _First, _Iter _Last, forward_iterator_tag)
/*
单向迭代器
*/
{
if (_Buy(_STD distance(_First, _Last)))
{
try
{
this->_Mylast() = _Ucopy(_First, _Last, this->_Myfirst());
}
catch(...)
{
_Tidy();
throw;
}
}
}
void _Construct_n(size_type _Count, const value_type *_Pval)
{
if (_Buy(_Count))
{
try
{
this->_Mylast() = _Ufill(this->_Myfirst(), _Count, _Pval);
/*
一个一个构造对象,如果抛了异常,catch之后,把已经构造的析构掉,throw
成功后返回尾指针
*/
}
catch (...)
{
/*
清理内存并reraise
*/
_Tidy();
throw;
}
}
}
vector(_Myt&& _Right) _NOEXCEPT
: _Mybase(_STD move(_Right._Getal()))
{ // construct by moving _Right
_Assign_rv(_STD forward<_Myt>(_Right), true_type());
}
vector(_Myt&& _Right, const _Alloc& _Al)
: _Mybase(_Al)
{ // construct by moving _Right, allocator
_Assign_rv(_STD forward<_Myt>(_Right));
}
_Myt& operator=(_Myt&& _Right)
_NOEXCEPT_OP(_Alty::propagate_on_container_move_assignment::value
|| _Alty::is_always_equal::value)
{ // assign by moving _Right
if (this != &_Right)
{ // different, assign it
_Tidy();
if (_Alty::propagate_on_container_move_assignment::value
&& this->_Getal() != _Right._Getal())
this->_Move_alloc(_Right._Getal());
_Assign_rv(_STD forward<_Myt>(_Right));
}
return (*this);
}
void _Assign_rv(_Myt&& _Right, true_type)
/*
可以传递所有权
*/
{
this->_Swap_all((_Myt&)_Right);
/*
交换所有的迭代器链表
*/
this->_Myfirst() = _Right._Myfirst();
this->_Mylast() = _Right._Mylast();
this->_Myend() = _Right._Myend();
/*
直接传递所有权
*/
_Right._Myfirst() = pointer();
_Right._Mylast() = pointer();
_Right._Myend() = pointer();
}
void _Assign_rv(_Myt&& _Right, false_type)
/*
(也许)不能直接交换memory所有权的情况
*/
{
if (get_allocator() == _Right.get_allocator())
{
_Assign_rv(_STD forward<_Myt>(_Right), true_type());
/*
两个迭代器相同的话,也直接交换所有权
*/
}
else
{
_Construct(_STD make_move_iterator(_Right.begin()),
_STD make_move_iterator(_Right.end()));
/*
不能直接交换两块memory的所有权
退化为拷贝构造,move_iterator对_Construct并没有卵用
*/
}
}
void _Assign_rv(_Myt&& _Right)
/*
右值移动构造
*/
{
_Assign_rv(_STD forward<_Myt>(_Right),
typename _Alty::propagate_on_container_move_assignment());
/*
propagate_on_container_move_assignment
这个tag标志memory的所有权是否可以在移动的时候被传递
*/
}
void push_back(value_type&& _Val)
{
if (_Inside(_STD addressof(_Val)))
/*
要push_back的元素是容器内部的
*/
{
size_type _Idx = _STD addressof(_Val) - this->_Myfirst();
if (this->_Mylast() == this->_Myend())
{
_Reserve(1);
}
_Orphan_range(this->_Mylast(), this->_Mylast());
this->_Getal().construct(this->_Mylast(),
_STD forward<value_type>(this->_Myfirst()[_Idx]));
++this->_Mylast();
}
else
{ // push back a non-element
if (this->_Mylast() == this->_Myend())
{
_Reserve(1);
}
_Orphan_range(this->_Mylast(), this->_Mylast());
this->_Getal().construct(this->_Mylast(),
_STD forward<value_type>(_Val));
++this->_Mylast();
}
}
iterator insert(const_iterator _Where, _Ty&& _Val)
{
return (emplace(_Where, _STD move(_Val)));
/*
通过移动构造把元素放进_Where
*/
}
template<class... _Valty>
void emplace_back(_Valty&&... _Val)
/*
用变长参数列表在列表尾部构造
*/
{
if (this->_Mylast() == this->_Myend())
{
_Reserve(1);
}
_Orphan_range(this->_Mylast(), this->_Mylast());
this->_Getal().construct(this->_Mylast(),
_STD forward<_Valty>(_Val)...);
++this->_Mylast();
}
template<class... _Valty>
iterator emplace(const_iterator _Where, _Valty&&... _Val)
{
size_type _Off = _VIPTR(_Where) - this->_Myfirst();
#if _ITERATOR_DEBUG_LEVEL == 2
if (size() < _Off)
_DEBUG_ERROR("vector emplace iterator outside range");
#endif /* _ITERATOR_DEBUG_LEVEL == 2 */
emplace_back(_STD forward<_Valty>(_Val)...);
/*
先把元素构造在末尾
*/
_STD rotate(begin() + _Off, end() - 1, end());
/*
然后循环移动
*/
return (begin() + _Off);
}
vector(_XSTD initializer_list<value_type> _Ilist, const _Alloc& _Al = allocator_type())
: _Mybase(_Al)
{
_Construct(_Ilist.begin(), _Ilist.end());
}
/*
从initializer list构造
*/
_Myt& operator=(_XSTD initializer_list<value_type> _Ilist)
{
assign(_Ilist.begin(), _Ilist.end());
return (*this);
}
/*
从initlalizer list赋值
*/
void assign(_XSTD initializer_list<value_type> _Ilist)
{
assign(_Ilist.begin(), _Ilist.end());
}
/*
从initializer list assign
*/
iterator insert(const_iterator _Where, _XSTD initializer_list<value_type> _Ilist)
{
return (insert(_Where, _Ilist.begin(), _Ilist.end()));
}
/*
插入
*/
~vector() _NOEXCEPT
{
_Tidy();
}
_Myt& operator=(const _Myt& _Right)
/*
copy assignment
*/
{
if (this != &_Right)
{
if (this->_Getal() != _Right._Getal()
&& _Alty::propagate_on_container_copy_assignment::value)
{
_Tidy();
this->_Copy_alloc(_Right._Getal());
}
/*
如果需要
拷贝之前先拷贝allocator
*/
this->_Orphan_all();
if (_Right.empty())
{
clear();
}
/*
right为空,直接清空
*/
else if (_Right.size() <= size())
{
/*
right的有效长度<=this的有效长度
*/
pointer _Ptr = _Copy_impl(_Right._Myfirst(),
_Right._Mylast(), this->_Myfirst());
/*
根据各种不同的迭代器类型实现的范围拷贝
返回已经用掉的部分的尾指针
*/
_Destroy(_Ptr, this->_Mylast());
/*
析构掉从尾指针开始到last的部分
*/
this->_Mylast() = this->_Myfirst() + _Right.size();
}
else if (_Right.size() <= capacity())
{
/*
right的有效长度>this的有效长度,小于总容量
*/
pointer _Ptr = _Right._Myfirst() + size();
_Copy_impl(_Right._Myfirst(),
_Ptr, this->_Myfirst());
/*
有效长度内的,拷贝
*/
this->_Mylast() = _Ucopy(_Ptr, _Right._Mylast(),
this->_Mylast());
/*
有效长度外的,构造
*/
}
else
{
/*
没有足够空间
*/
if (this->_Myfirst() != pointer())
{
_Destroy(this->_Myfirst(), this->_Mylast());
this->_Getal().deallocate(this->_Myfirst(), this->_Myend() - this->_Myfirst());
/*
清理掉原来的
*/
}
if (_Buy(_Right.size()))
{
try
{
this->_Mylast() = _Ucopy(_Right._Myfirst(), _Right._Mylast(), this->_Myfirst());
}
catch(...)
{
_Tidy();
throw;
}
}
/*
照常拷贝
*/
}
}
return (*this);
}