自定义vector容器【未完待续】

#ifndef REDEFINE_VECTOR_H_
#define REDEFINE_VECTOR_H_
#include"stdafx.h"
#include<memory>
#include<cstring>
#include<fstream>
#include<cstdlib>
template<typename  T,typename allocator=std::allocator<T> >
class vector
{
//public:
//	typedef T value_type;
//	typedef value_type* pointer;
//	typedef const value_type const_pointer;
//	typedef value_type& reference;
//	typedef const value_type& const_reference ;
//	typedef std::size_t size_type;
//	typedef std::ptrdiff diff_type;
public:
	vector();
	vector( int num );
	vector(  resize(int num) );
	vector(T* start, T* end );
	
	void assign(int count, T value );
	void assign(T* start, T*  end );
	
	T& at(int  Pos);
	const T&  at(int  Pos) const;

	const T&   back( )const;
	T& back();

	int capcity()const;
	void clear();
	bool empty()const;

	T* begin();
	T* end();
	
	const T& front()const;
	T& front();

	void insert( T* pos, const T value );
	void insert( T* pos, int count, const T value );
	void insert( T* sour, T* first, T* last );

	void pop_back( );
	void push_back(const T value );

	void erase( T* pos );
	void erase( T* first , T* last );

	~vector();

private:
	static std::allocator<T>elem;
	T* head;
	T* curr;
	T* tail;
	void unload_handle( );// 转储
	const unsigned int resize( int num )const
	{ return  num; }
	void realloc_max(int size );
	void realloc_min( int size );
	void reallocate();
};
#endif


实现文件//


#include"stdafx.h"
#include"redefine_vector.h"
template<T>vector::vector<T>()
	{	
			T* head=elem.allocate(5);
			T* curr=head;
			T* tail=head+5;
			uninitialized_fill( head, tail, T() );
	}
template<T>vector::vector( int num )
	{
		T* head=elem.allocate(num);
		T* curr=head;
		T* tail=head+num;
		uninitialized_fill( head, tail, T() );
	}
template<T>vector::vector(  resize(int num) )
	{
		std::ptrdiff size=tail-head;
		if ( num>size )
			try
				{	
					realloc_max(num);
				}
		if ( num<size )
			try
				{
					realloc_min(num);
				}
	}
template<T>vector::vector(T* start, T* end )
	{
		std::ptrdiff sur_dis=end-start;
		std::ptrdiff des_dis=tail-head;
		if ( sur_dis>des_dis )
			reallocate();
		for ( T* p=curr; p!=head; )
			elem.destory( --p );
		if ( head )
			elem.deallocate( head, tail-head );
		for (T* tmp=start; tmp<=end; tmp++ )
		{
			elem.construct( curr, *tmp);
			++curr;
		}
	}
template<T> void vector::assign(int count, T value )
	{
		if ( count<=(tail-head) )
		{
			for ( T* p=curr; p!=head; )
				elem.destory( --p );
			if ( head )
				elem.deallocate( head, tail-head );
			uninitialized_fill( head, head+count, value );
		}
		else
		{
			realloc_max( count+20 );
			uninitialized_fill_n( head, head+10 , value, count );
		}
	}
template<T> void vector:: assign(T* start, T*  end )
	{
		std::ptrdiff count=end-start;
		if ( (end-start)<(tail-head) )
		{
			for ( T* p=curr; p!=head; )
				elem.destory( --p );
			if ( head )
				elem.deallocate( head, tail-head );
			memcpy( head, start, count );//c_memcpy(to ,from, count )
		}
		if ( (end-start) > (tail-head) )
		{
			realloc_max( count+20 );
			memcpy( head, start, count );
		}
	}
template<T> T& vector:: at(int  Pos)
{
	curr=head+pos;
	return  *curr;

}
template<T>const T& vector:: at(int  Pos) const
{
	static T* ins_pos=head;
	for ( ; ins_pos!=head+pos; ++ins_pos )
		;
	return *( ins_pos+1);
}
template<T> const T&  vector::  back( )const
{
	return *curr;
}
template<T> T& vector:: back()
{
	return *curr;
}
template<T> int vector:: capcity()const
{
	return ( tail-curr );
}
template<T> void  vector:: clear()
{
		for ( T* p=curr; p!=head; )
			elem.destory( --p );
}
template<T> bool  vector:: empty()const
 {
	 return ( (curr-head)==0 );
 }
template<T> T* vector::  begin()
	{
		return head;
	}
template<T> T* vector::  end()
	{
		return tail;
	}
template<T>const T&  vector:: front()const
	{
		if ( head )
			return *head;
	}
template<T> T& vector:: front()
	{
		if (head )
			return *head;
	}
template<T> void vector::  insert( T* pos, const T value ) //  越界异常
	{
		std::ptrdiff size=tail-head;
		std::ptrdiff dis=pos-head;
		if ( pos>tail )
		{
			cerr<<"Out of range \n ";
			exit(EXIT_FAILURE);
		}
		else
		{
			memmove(head+dis+1, head+dis, 1 )
			for ( T* ins_pos=head; ins_pos<=head+dis; ++ins_pos )
				;
			elem.construct( ins_pos, value );
		}
	}
template<T> void vector::  insert( T* pos, int count, const T value )//越界异常
	{
		std::ptrdiff  size=tail-head
		std::ptrdiff dis=pos-head;
		if ( dis>size )
		{
			cerr<<"Out of range \n ";
			exit(EXIT_FAILURE);
		}
		else
		{
			memmove(head+dis+count, head+dis , count );
			for ( T* ins_pos=head; ins_pos<=head+dis; ++ins_pos )
				;
			for ( int vc=0; vc<=count; ++vc )
				elem.construct( ins_pos, value );
			++ins_pos;
		}
	}
template<T>void vector:: insert( T* sour, T* first, T* last )// 越界异常
{
	std::ptrdiff sur_dis=last-first;
	std::ptrdiff des_dis=tail-head;
	if ( sur_dis<des_dis )
	{
		memmove( sour+sur_dis, sour, sur_dis );
		for ( T* ins_pos=head; ins_pos<=sour; ++ins_pos )
			;
		for ( T* ins_value=first; ins_value<=last; ++ins_value )
			elem.construct( ins_pos, *ins_value );
		++ins_pos;
	}
	else
	{
		cerr<<"Out of range \n ";
		exit( EXIT_FAILURE);
	}
}
template<T> void vector:: pop_back( )
	{
		if ( emepty() )
		{
			cerr<<"vector is empty \n ";
			return ;
		}
		else
		{
				T* pos=curr;
				cout<<*pos;
				curr--;
				elem.destory( pos );
		}
	}
template<T>void  vector:: push_back(const T value )
	{
		if ( curr=tail )
			re_allocate( ) ;
		elem.construct( curr, value );
		++curr;
	}
template<T> void vector:: reserve( int count )
	{
		realloc_max( count );
	}

template<T>void vector:: erase( T* pos )
		{
		std::ptrdiff position=pos-head;
		if ( head+position ==0 )
			
		{
			cerr<<"None elements exits "<<endl;
			exit(EXIT_FAILURE);
		}
		else
		{
			memmove( pos, pos+1, 1);
		}
	}
template<T>void vector::erase( T* first , T* last )//越界异常
{
	if ( first>=head && last<=tail )
	{
		std::ptrdiff size=last-first;
		std::ptrdiff start_dis=first-head;
		std::ptrdiff end_dis=last-head;
		memmove( head+start_dis, head+end_dis, size );
	}
	else
	{
		cerr<<"Out of range \n ";
		exit(EXIT_FAILURE);
	}
}
template<T> vector::~vector()
	{
		unload_hand();
		if ( head!=tail )
		{
			for ( T*  p=curr; p!=head; )
				elem.destory( --p );
			deallocate( head, tail-head );
		}
	}
/private member/
template<typename T >
void vector<T>::unload_handle()
{
	ofstream put( "temp.txt", ios::out | ios:: app );
	if ( put.is_open()==false )
	{
		cerr<<"open file failed \n ";
		exit(EXIT_FAILURE);
	}
	else
	{
		while (; curr!=head ; )
		put<<*curr--;
	}
	put.clear();
	put.close();
} 
template<T>void vector<T>::realloc_max( int size )
{
		std::ptrdiff dis=curr-head;
		T*  new_mem= elem.allocate( size );
		uninitialized_copy( head, curr, new_mem );
		for ( T* p=curr; p!=head; )
			elem.destory( --p );
		if ( head )
			elem.deallocate(head, tail-head );
		head=new_men;
		curr=head+dis;
		tail=head+size;
}
template<T>void vector<T>::realloc_min( int size )
{
	T* new_mem=elem.allocate(size);
	uninitialized_copy( head, head+size, new_mem );
	for ( T* p=curr; p!=head; )
			elem.destory( --p );
	if ( head )
			elem.deallocate(head, tail-head );
	head=new_mem;
	curr=head+size;
	tail=head+size;
}
template<T> void vector<T>:: reallocate( )
{
	std::ptrdiff size=curr-head;
	std::ptrdiff new_size=2*size;
	T* new_mem=elem.allocate( new_size )
		uninitialized_copy( head, curr, new_mem );
	for ( T* p=curr ; p!=head;  )
		elem.destory( --p );
	if ( head );
	elem.deallocate( head, tail-head );
	head=new_men;
	curr=head+size;
	tail=head+new_size;
}
catch(std:: bad_alloc& error )
	{
		unload_hand();
		cerr<<"memory allocate error  "<<error.what()<<endl;
		throw;
	}
catch(...){ }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值