8.C++ STL之vector

一、容器vector

1、vector结构

在这里插入图片描述

template <class T,class Alloc=alloc>
class vector{
public:
	typedef T value_type;
	typedef value_type* iterator; //T*
	typedef value_type& reference;
	typedef size_t size_type;
protected:
	iterator start;
	iterator finish;
	iterator end_of_storage;
public:
	iterator begin(){return start;}
	iterator end() {return finish;}
	size_type size() const {return size_type(end()-begin());}
	size_type capacity() const {return size_type(end_of_storage -begin);}
	bool empty() const {return begin()==end();}
	reference operator[](size_type n){return *(begin()+n)}
	reference front(){return *begin();}
	reference back(){return *(end()-1);}
};

2、两倍增长
void push_back(const T& x){
	if(finish!=end_of_storage){//尚有备用空间
		construct(finish,x);//全局函数
		++finish;//调整水位高度
	}
	else//已无备用空间
		insert_aux(end(),x);
}

template <class T,class Alloc=alloc>
void vector<T,Alloc>::insert_aux(iterator position,const T&x){
	if(finish!=end_of_storage){//尚有备用空间
	/*
	为什么此处的代码与push_back一样?因为本函数除了被push_back调用,
	还会被其他函数调用,因此仍需要被检查
	*/
	//在备用空间起始处构建一个元素,并以vector最后一个元素为其初始值
		construct(finish,*(finish-1));
		++finish;//调整水位
		T x_copy=x;
		copy_backward(position,finish-2,finish-1);
		*position=x_copy;
	}
	else{//已无备用空间
		const size_type old_size=size();
		const size_type len=old_size!=0?2*old_size:1;
		//以上分配原则:如果原大小为0,则分配1(个元素大小)
		//如果原大小不为0,则分配原大小的两倍
		//前半段用来放置原数据,后半段准备用来放置新数据。

		iterator new_start=data_allocator::allocate(len);
		iterator new_finish=new_start;
		try{
		//将原vector的内容拷贝到心vector
		new_finish=uninitialized_copy(start,position,new start);
		construct(new_finisgh,x);//为新元素设初值x
		++new_finish;//调整水位
		/*拷贝安插点后的原内容(因为本函数也可能被insert(p,x)调用,
		在第n个元素插入一个新元素,这时候存在安插点之前和安插点之后的两段)*/
		new_finish=uninitialized_copy(position,finish,new_finish);	
		}
		catch(...){
			//"commit or rollback"
			destory(new_start,new_finish);
			data_allocator::deallocate(new_start,len);
			throw;
		}
		
		//解构并释放原vector
		destory(begin(),end());
		deallocate();
		//调整迭代器,指向新的vector
		start=new_start;
		finish=new_finish;
		end_of_storage=new_start+len;
	}
};

3、vector的迭代器iterator

在这里插入图片描述

//G 2.9
template<class T,class Alloc=alloc>
class vector{
	typedef T value_type;
	typedef value_type* iterator;//T*
	...
};

vector<int> vec;
vector<int>::iterator ite=vec.begin():

在这里插入图片描述

  • vector<_Tp>的大小即其父类_Vector_base<_Tp>的大小;
  • 父类_Vector_base<_Tp>的大小取决于成员_Vector_impl数据类型_M_impl的大小,即类_Vector_impl<_Tp>的大小
  • 类_Vector_impl<_Tp>有三个指针,3*4=12,加上父类std::allocator<_Tp>的大小(0)=12
//G4.9
template<typename _Tp,typename _Alloc=std::allocator<_Tp>>
class vector:protected _Vector_base<_Tp,_Alloc>
{
	...
	typedef _Vector_base<_Tp,_Alloc> _Base;
	typedef typename _Base::pointer pointer;
	typedef _gnu_cxx::_normal_iterator<pointer,vector> iterator;//类_normal_iterator
};

//类_normal_iterator
using std::iterator_traits;
using std::iterator;
template<typename _Iterator,typename _Container>
class _normal_iterator
{
protected:
	_Iterator _M_current;
	typedef iterator_traits<_Iterator> _traits_type;
public:
	typedef _Iterator iterator_type;
	typedef typename__traits_type::iterator_category iterator_category;
	typedef typename__traits_type::value_type value_type;
	typedef typename__traits_type::difference_type difference_type;
	typedef typename__traits_type::reference reference;
	typedef typename__traits_type::pointer pointer;
/*
	_GLIBCXX_CONSTEXPR_normal_iterator() _GLIBCXX_NOEXCEPT:_M_current(_Iterator()){}
*/		

template<typename _Tp,typename _Alloc>
struct _Vector_base
{
	typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template rebind<_Tp>::other _Tp_alloc_type;
	typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>::pointer pointer; 
}

template<typename _Tp>
class allocator:public _glibcxx_base_allocator<_Tp>
{
public:
	typedef size_t size_type;
	typedef ptrdiff_t difference_type;
	typedef _Tp* pointer;
	typedef const _Tp* const_pointer;
	typedef _Tp& reference;
	typedef const _Tp& const_reference;
	typedef _Tp value_type;
...
	
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值