内容
#ifndef VECTOR_H_
#define VECTOR_H_
#include<cstdlib>
namespace VECTOR
{
template<typename Ty>
class Vector
{
public:
//构造,析构
explicit Vector(const int def = 0);
Vector(const Vector&);
explicit Vector(const int, const Ty&);
~Vector();
//存取
void push_back(const Ty&);
void pop_back();
//索引
Ty& operator[](const int);
const Ty& operator[](const int)const;
//查找,返回索引,没有返回-1
const int find(const Ty&)const;
//返回当前占用
const int size(void)const;
//定义容量
void resize(const int);
private:
//把b中的前size个元素拷贝到a
static void Copy(Ty* a,const int size, const Ty* b);
//容量提升处理
const int upcapacity(const int size);
//判断是否为空
const bool empty();
//判断是否满
const bool full();
//扩容
void change(const int);
Ty* m_value;//储存器
int m_size;//当前占用
int m_capacity;//容量
};
}
#endif // !Vector_H_
实现
namespace VECTOR
{
//创建def容量
template<typename Ty>
VECTOR::Vector<Ty>::Vector(const int def)
{
this->m_size = 0;
this->m_value = new Ty[this->upcapacity(def)];
}
//创建n个t的容器
template<typename Ty>
VECTOR::Vector<Ty>::Vector(const int n, const Ty& t)
{
this->m_size = n;
this->m_value = new Ty[this->upcapacity(n)];
for (int i = 0; i < this->m_size; ++i)
(this->m_value)[i] = t;
}
//拷贝构造,不用容量提升
template<typename Ty>
VECTOR::Vector<Ty>::Vector(const Vector<Ty>& ve)
{
this->m_size = ve.m_size;
this->m_capacity = ve.m_capacity;
this->m_value = new Ty[this->m_capacity];
Copy(this->m_value,ve.m_size, ve.m_value);
}
//析构
template<typename Ty>
VECTOR::Vector<Ty>::~Vector()
{
this->m_capacity = 0;
this->m_size = 0;
delete[] (this->m_value);
this->m_value = nullptr;
}
//尾插
template<typename Ty>
void VECTOR::Vector<Ty>::push_back(const Ty& t)
{
if (this->full())
this->change(this->upcapacity(this->m_capacity));
(this->m_value)[(this->m_size)++] = t;
}
//尾删
template<typename Ty>
void VECTOR::Vector<Ty>::pop_back()
{
if (this->empty())
exit(EXIT_FAILURE);
--(this->m_size);
}
//索引
template<typename Ty>
Ty& VECTOR::Vector<Ty>::operator[](const int n)
{
if (n <= 0 || n > this->m_size)
exit(EXIT_FAILURE);
return (this->m_value)[n];
}
template<typename Ty>
const Ty& VECTOR::Vector<Ty>::operator[](const int n) const
{
if (n <= 0 || n > this->m_size)
exit(EXIT_FAILURE);
return (this->m_value)[n];
}
//查找
template<typename Ty>
const int VECTOR::Vector<Ty>::find(const Ty& t) const
{
for (int i = 0; i < this->m_size; ++i)
if ((this->m_value)[i] == t)
return i;
return -1;
}
//返回占用
template<typename Ty>
const int VECTOR::Vector<Ty>::size(void) const
{
return this->m_size;
}
//定义容量
template<typename Ty>
void VECTOR::Vector<Ty>::resize(const int n)
{
if (n <= 0)
exit(EXIT_FAILURE);
if (n < this->m_size)
this->m_size = n;
this->m_capacity = n;
this->change(n);
}
//拷贝
template<typename Ty>
void VECTOR::Vector<Ty>::Copy( Ty* a ,const int size, const Ty* b)
{
for (int i = 0; i < size; ++i)
a[i] = b[i];
}
//空
template<typename Ty>
const bool VECTOR::Vector<Ty>::empty()
{
if (this->m_size == 0)
return true;
return false;
}
//满
template<typename Ty>
const bool VECTOR::Vector<Ty>::full()
{
if (this->m_size == this->m_capacity)
return true;
return false;
}
//容量抬升
template<typename Ty>
const int VECTOR::Vector<Ty>::upcapacity(const int size)
{
return this->m_capacity = size * 2 + 5;
}
//变容
template<typename Ty>
void VECTOR::Vector<Ty>::change(const int capacity)
{
Ty* temp = new Ty[capacity];
if (this->m_capacity < capacity)
Copy(temp, this->m_size, this->m_value);
else
Copy(temp, capacity, this->m_value);
delete[]this->m_value;
this->m_value = temp;
}
}