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

本文介绍了一个自定义向量类的设计与实现细节,包括构造函数、成员函数等,并提供了完整的源代码。该向量类支持多种操作,如插入、删除、分配内存等。

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

#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(...){ }

#include "ContactPerson.h" #include <iostream> #include"Person.h" #ifndef ContactPerson.cpp #include<string> #include<vector> #include<fstream> #include <algorithm> #include<sstream> using namespace std; void contact_Person::Menu() { //菜单 ; cout << "=======================================================================" << endl; cout << "=========================== 联系人管理系统 ============================" << endl; cout << "=======================================================================" << endl; cout << "|| 1. 添加人员 ||" << endl; cout << "|| 2. 按姓名电话查询 ||" << endl; cout << "|| 3. 按地址模糊查询 ||" << endl; cout << "|| 4. 按类别查询 ||" << endl; cout << "|| 5. 按姓名排序输出 ||" << endl; cout << "|| 6. 修改信息 ||" << endl; cout << "|| 7. 按姓名电话删除 ||" << endl; cout << "|| 8. 显示所有联系人 ||" << endl; cout << "|| 0. 退出系统 ||" << endl; cout << "=======================================================================" << endl; } void contact_Person::output() { //输入功能; string name1, sex, address, postal_code1, classification,qq; string telephone; string chiose, Letter_box2; ofstream mycout("D:\\Flie.txt",ios::app); if (!mycout) { cout << "打开文件失败" << endl; } else { do { cout << "请输入名字" << " "; cin >> name1; std::system("cls"); cout << "请输入性别" << " "; cin >> sex; std::system("cls"); cout << "请输入电话" << " "; cin >> telephone; std::system("cls"); cout << "请输入地址" << " "; cin >> address; std::system("cls"); cout << "请输入邮编" << " "; cin >> postal_code1; std::system("cls"); cout << "请输入邮箱" << " "; cin >> Letter_box2; std::system("cls"); cout << "请输入QQ号" << " "; cin >> qq; std::system("cls"); cout << "请输入类别" << " "; cin >> classification; std::system("cls"); mycout << "名字" << name1 << " " << "性别" << sex << " " << " 电话" << telephone << " " << "地址" << address << " " << "邮编" << postal_code1 << " " << " 邮箱" << Letter_box2 << " " << "QQ号" << qq << " " << "类别" << classification << endl; ifstream mycin("D:\\Flie.txt", ios::in); if (!mycin) { cout << "读取失败" << endl; } //将读取的文件存放到一个line变量 string line; while (getline(mycin, line)) { istringstream iss(line); iss >> name1 >> sex >> address >> classification >> Letter_box2 >> postal_code1 >> qq >> telephone; //这是把输入的变量先存放到一个新的contact_Person类的对象, // 然后把这个新的对象作为被读取的值传给一个contact_Person类型的contact容器中; contact_Person newcontact_Person(name1, sex, address, classification, Letter_box2, postal_code1, qq, telephone); contact.push_back(newcontact_Person); contact.size(); } cout << " 添加成功" << endl; cout << " 是否继续输入y(是)/n(否)" << endl; cin >> chiose; } while (chiose != "n"); mycout.close(); } } void contact_Person::Query_function1() { string chiose; //查询功能1(姓名·电话查询信息); do { cout << "请选择1名字查寻,2电话查询" << endl; int a; cin >> a; if (a == 1) { cout << " 请输入要查询的人名字" << endl; string NAME; cin >> NAME; //用find_if查找(find_if,为algorithm库中函数),遍历容器contact(容器contact在输入是已经输入过值) //此时只需找到name==NAME; auto it = find_if(contact.begin(), contact.end(), [&NAME]( contact_Person& obj) { return obj.Name == NAME; }/*这里是所要查询的元素,返回contact_Person类中和输入相同的name,作为find_if的需查询内容的参数*/); if (it != contact.end()) { // 找到 contact_Person& found = *it; // 解引用迭代器,得到对象 std::system("cls"); cout << "姓名" << found.Name << " " << "性别" << found.Sex << " " << "电话" << found.TelePhone << " " << "地址" << found.Address << " " << "邮编" << found.Postal_code << " " << " 邮箱" << found.Letter_box << " " << "QQ号" << found.QQ << " " << "类别" << found.Classification << endl; } else { cout << "未找到姓名为 [" << NAME << "] 的联系人" << endl; system("pause"); } system("cls"); cout << "是否继续查询y(是)/n(否)" << endl; cin >> chiose; } else if (a == 2) { // 电话查询 cout << "请输入要查询的人电话" << endl; string TELEPHONE; cin >> TELEPHONE; //用find_if查找(find_if, 为algorithm库中函数), // 遍历容器contact(容器contact在输入是已经输入过值) auto IT = find_if(contact.begin(), contact.end(), [&TELEPHONE]( contact_Person& obj2) { return obj2.TelePhone == TELEPHONE; }); if (IT != contact.end()) { contact_Person& found = *IT; std::system("cls"); cout << "姓名" << found.Name << " " << "性别" << found.Sex << " " << "电话" << found.TelePhone << " " << "地址" << found.Address << " " << "邮编" << found.Postal_code << " " << " 邮箱" << found.Letter_box << " " << "QQ号" << found.QQ << " " << "类别" << found.Classification << endl; } else { cout << "未找到电话为 [" << TELEPHONE << "] 的联系人" << endl; system("pause"); } system("cls"); cout << "是否继续查询y(是)/n(否)" << endl; cin >> chiose; } } while (chiose != "n" ); } //查询功能2(地址模糊查寻) ; void contact_Person::Query_function2() { string chiose; do { string ADDRESS; cout << " 请输入地址" << endl; cin >> ADDRESS; //用find_if查找(find_if, 为algorithm库中函数), // 遍历容器contact(容器contact在输入是已经输入过值) auto IT2 = find_if(contact.begin(), contact.end(), [&ADDRESS]( contact_Person obj3) { return obj3.Address == ADDRESS; }); if (IT2 != contact.end()) { contact_Person& found = *IT2; std::system("cls"); cout << "姓名" << found.Name << " " << "性别" << found.Sex << " " << "电话" << found.TelePhone << " " << "地址" << found.Address << " " << "邮编" << found.Postal_code << " " << " 邮箱" << found.Letter_box << " " << "QQ号" << found.QQ << " " << "类别" << found.Classification << endl; } else { cout << "未找到地址为 [" << ADDRESS << "] 的联系人" << endl; } cout << "是否继续查询y(是)/n(否)" << endl; cin >> chiose; } while (chiose != "n"); } //查询功能3(类别查询); void contact_Person::Query_function3() { string chiose; do { string CLASSIFICATION; cout << " 请输入类别" << endl; cin >> CLASSIFICATION; //find_if( auto TI3 = find_if(contact.begin(), contact.end(), [&CLASSIFICATION]( contact_Person& obj4){ return obj4.Classification == CLASSIFICATION; }); if (TI3 != contact.end()) { contact_Person& found4 = *TI3; std::system("cls"); cout << "姓名" << found4.Name << " " << "性别" << found4.Sex << " " << "电话" << found4.TelePhone << " " << "地址" << found4.Address << " " << "邮编" << found4.Postal_code << " " << " 邮箱" << found4.Letter_box << " " << "QQ号" << found4.QQ << " " << "类别" << found4.Classification << endl; } else { cout << "未找到类别为 [" << CLASSIFICATION << "] 的联系人" << endl; } cout << "是否继续查询y(是)/n(否)" << endl; cin >> chiose; } while (chiose != "n"); } //根据姓名排序输出(名字首字母顺序); void contact_Person::Name_sorting_output() { } void contact_Person::Alter() { //修改信息; cout << "请输入你要修改的内容" << endl; string name, sex, Address, QQ, Classification; string Postal_code, TlePhone; cout << "请选择修改的字段:" << endl; cout << "1: 姓名" << endl; cout << "2: 性别" << endl; cout << "3: 电话" << endl; cout << "4: 地址" << endl; cout << "5: 邮编" << endl; cout << "6: 邮箱" << endl; cout << "7: QQ" << endl; cout << "8: 类别" << endl; int choice; cin >> choice; switch (choice) { case 1: cout << "请输入修改后的姓名" << endl; cin >> name; break; case 2: cout << "请输入修改后的性别" << endl; break; case 3: default: break; } } //未完待续 void contact_Person::Name_TelePhone_Alter() {} //根据姓名·电话修改; void contact_Person::Name_TelePhone_Delete() {} //根据姓名·电话删除 ; void contact_Person::Dispiay() { cout << " " << endl; system("cls"); cout << "______________________________这是从文件中读取的————————————————" << endl; ifstream mycin("D:\\Flie.txt", ios::in); if (!mycin) { cout << "读取失败" << endl; } string line; while (getline(mycin, line)) { // 读取整行内容 cout << line << endl; } mycin.close(); cout << "____________________________________________________________________________" << endl; cout << "__________________________下面是读取的一个string容器contac2的内容———————————————" << endl; ifstream mycin1("D:\\Flie.txt", ios::in); if (!mycin1) { cout << "读取失败" << endl; } string line1; while (getline(mycin1, line1)) { contact2.push_back(line1); for ( auto& str : contact2) { cout << str << endl; contact2.clear(); } } mycin.close(); system("pause"); system("cls"); } #endif // !Contact_Person.cpp
最新发布
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值