手写实现vectorC++

#pragma once
#include 
#include
using namespace std;
template
class vector
{
public:
	typedef T value_type;
	typedef T* iterator;
	vector() : _data(NULL), _size(0), _capacity(0) {};
	vector(size_t a);
	vector(size_t a, T b);
	vector(const vector& vec);
	bool operator==(const vector& vec) const;
	vector& operator=(const vector& vec);
	value_type& operator[](size_t index);
	void push_back(value_type val);
	void insert(iterator it, value_type val);
	void pop_back();
	void erase(iterator it);
	value_type& front();
	value_type& back();
	size_t size() const;
	size_t capactiyt()const;
	bool empty();
	~vector();
private:
	value_type* _data;
	size_t _size;
	size_t _capacity;
};

//指定大小的有参构造函数
template
vector::vector(size_t n)
{
	_data = new value_type[n + 10];
	_capacity = n + 10;
	_size = n;
}

//析构函数
template
vector::~vector()
{
	delete _data;
	_data = NULL;
	_size = 0;
	_capacity = 0;
}

//指定大小和初始值的有参构造函数
template
vector::vector(size_t a, T b)
{
	_capacity = a + 10;
	_data = new value_type[_capacity];
	for (int i = 0; i < a; i++)
	{
		_data[i] = b;
	}
	_size = a;
}

//拷贝构造函数
template
vector::vector(const vector& vec)
{
	_size = vec._size;
	_capacity = vec._capacity;
	_data = new value_type[_capacity];
	for (int i = 0; i < _size; i++)
	{
		_data[i] = vec._data[i];
	}
}

//重载 == 运算符
template
bool vector::operator==(const vector& vec) const 
{
	if (_size != vec._size) return false;
	for (int i = 0; i < _size; i++)
	{
		if (_data[i] != vec._data[i]) return false;
	}
	return true;
}

//重载等于号运算符为什么要加
template
vector& vector::operator=(const vector& vec)
{
	if (this == &vec) return *this;
	delete[]_data;
	_data = new value_type[vec._capacity];
	for (int i = 0; i < _size; i++)
	{
		_data[i] = vec._data[i];
	}
	_size = vec._size;
	_capacity = vec._capacity;
	return *this;
}

//重载[]运算符当函数声明或定义在类模板之外时,需要使用 typename 关键字来指示 value_type 是一个类型
template
typename vector::value_type& vector::operator[](size_t index)
{
	if (index > _size - 1) cout << "OUT OF RANGE" ;
	return _data[index];
}

//增添元素push_back()
template
void vector::push_back(value_type val)
{
	if (0 == _capacity)
	{
		_capacity = 10;
		_data = new value_type[_capacity];
	}
	else if (_size + 1 > _capacity)
	{
		_capacity *= 2;
		value_type* temp = new value_type[_capacity];
		for (int i = 0; i < _size; i++)
		{
			temp[i] = _data[i];
		}
		delete[]_data;
		_data = temp;
	}
	_data[_size++] = val;
}

//插入元素
template
void vector::insert(iterator it, value_type val)
{
	int index = it - _data;
	if (_capacity == 0)
	{
		_capacity = 10;
		_data = new value_type[_capacity];
		_data[0] = val;
	}
	else if (_size + 1 > _capacity)
	{
		_capacity *= 2;
		value_type* temp = new value_type[_capacity];
		for (int i = 0; i < index; i++)
		{
			temp[i] = _data[i];
		}
		temp[index] = val;
		for (int i = index; i < _size; i++)
		{
			temp[i + 1] = _data[i];
		}
		delete[]_data;
		_data = temp;
	}
	else
	{
		for (int i = _size - 1; i >= index; i--)
		{
			_data[i + 1] = _data[i];
		}
		_data[index] = val;
	}
	++_size;
}

//尾删元素
template
void vector::pop_back()
{
	if (_size == 0) cout << "OUT OF RANGE";
	else
	{
		--_size;
	}
}

//删除元素
template
void vector::erase(iterator it)
{
	int index = it - _data;
	for (int i = index; i < _size - 1; ++i)
	{
		_data[i] = _data[i + 1];
	}
	--_size;
}

//返回首元素
template
typename vector::value_type& vector::front()
{
	return _data[0];
}

//返回尾部元素
template
typename vector::value_type& vector::back()
{
	return _data[_size - 1];
}

//返回大小
template
size_t vector::size() const
{
	return _size;
}

//返回容量
template
size_t vector::capactiyt() const
{
	return _capacity;
}

//是否为空
template
bool vector::empty()
{
	if (_size == 0) return true;
	else
		return false;
}

里面的几个问题,重载=号返回引用

typename vector::value_type&前面必须添加typename,当函数声明或定义在类模板之外时,需要使用 typename 关键字来指示 value_type 是一个类型

还有模板类没办法分文件编写

常量对象以及常量对象的指针引用等只能调用常量成员函数——C++primer P232

你可能感兴趣的:(c++,算法,开发语言)