VS 2010 STL vector源码分析

本文详细剖析了C++标准模板库(STL)中的vector容器的内部实现机制,包括类结构框架、迭代器原理、内存分配策略及关键成员函数的工作流程。通过源码解读,阐述了vector如何高效管理动态数组,以及在插入、删除等操作中如何保持性能和安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

类结构框架

vector继承于_Vector_val,继承于_Container_base,_Container_base在debug模式下(只探索debug模式)就是如此定义:typedef _Container_base12 _Container_base;这里面最重要的就是定义了一个叫做“容器代理”的成员变量_Container_proxy *_Myproxy;这个“代理”,就是在container与iterator之间代理一下下。

		// CLASS _Container_proxy
struct _Container_proxy
	{	// store head of iterator chain and back pointer
	_Container_proxy()
		: _Mycont(0), _Myfirstiter(0)
		{	// construct from pointers
		}

	const _Container_base12 *_Mycont;//指向容器,这里其实指向我们的vector
	_Iterator_base12 *_Myfirstiter;  //指向第一个迭代器
	};

所以我们先来看_Container_base12。 

struct _CRTIMP2_PURE _Container_base12
	{	// store pointer to _Container_proxy
public:
	_Container_base12()
		: _Myproxy(0)
		{	// construct childless container
		}

	_Container_base12(const _Container_base12&)
		: _Myproxy(0)
		{	// copy a container
		}

	_Container_base12& operator=(const _Container_base12&)
		{	// assign a container
		return (*this);
		}

	~_Container_base12()
		{	// destroy the container
		_Orphan_all();
		}

	_Iterator_base12 **_Getpfirst() const
		{	// get address of iterator chain
		return (_Myproxy == 0 ? 0 : &_Myproxy->_Myfirstiter);
		}

	void _Orphan_all()	// orphan all iterators
	{	// orphan all iterators
 #if _ITERATOR_DEBUG_LEVEL == 2
	if (_Myproxy != 0)
		{	// proxy allocated, drain it
		_Lockit _Lock(_LOCK_DEBUG);
    
        //下面两步就是把自己(container)与所有的和自己相关的iterator断绝关系
		for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
			*_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter)
			(*_Pnext)->_Myproxy = 0;//1,把所有的iterator的proxy置0
		_Myproxy->_Myfirstiter = 0;//2,把链表中第一个迭代器指针置0
		}
 #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
	}
	void _Swap_all(_Container_base12&);	// swap all iterators

	_Container_proxy *_Myproxy;
	};

_Vector_val类里面最重要的是几个变量,看注释应该很容易理解。比如一个vector分配了十个int大小内存,但是只有前三个赋了值,那么_Myfirst指向第0号元素,_Mylast指向3号元素,_Myend指向10号元素(其实不存在此元素)。_Alval是内存分配器,反正大致就是替换new的意思。

		// TEMPLATE CLASS _Vector_val
template<class _Ty,
	class _Alloc>
	class _Vector_val
		: public _Container_base
	{	// base class for vector to hold data
public:
	typedef typename _Alloc::template rebind<_Ty>::other _Alty;
	typedef typename _Alty::size_type size_type;
	typedef typename _Alty::difference_type difference_type;
	typedef typename _Alty::pointer pointer;
	typedef typename _Alty::const_pointer const_pointer;
	typedef typename _Alty::reference reference;
	typedef typename _Alty::const_reference const_reference;
	typedef typename _Alty::value_type value_type;

	pointer _Myfirst;	// pointer to beginning of array
	pointer _Mylast;	// pointer to current end of sequence
	pointer _Myend;	// pointer to end of array
	_Alty _Alval;	// allocator object for values
	};

 

		// TEMPLATE CLASS vector
template<class _Ty,
	class _Ax = allocator<_Ty> >
	class vector
		: public _Vector_val<_Ty, _Ax>
	{	// varying size array of values
public:
	typedef vector<_Ty, _Ax> _Myt;
	typedef _Vector_val<_Ty, _Ax> _Mybase;
	typedef typename _Mybase::_Alty _Alloc;

	typedef _Alloc allocator_type;
	typedef typename _Alloc::size_type size_type;
	typedef typename _Alloc::difference_type difference_type;
	typedef typename _Alloc::pointer pointer;
	typedef typename _Alloc::const_pointer const_pointer;
	typedef typename _Alloc::reference reference;
	typedef typename _Alloc::const_reference const_reference;
	typedef typename _Alloc::value_type value_type;

 #define _VICONT(it)	it._Getcont()
 #define _VIPTR(it)	(it)._Ptr

	typedef _Vector_iterator<_Mybase> iterator;
	typedef _Vector_const_iterator<_Mybase> const_iterator;

	typedef _STD reverse_iterator<iterator> reverse_iterator;
	typedef _STD reverse_iterator&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值