#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
template <typename T>
class My_vector
{
private:
T *start;
T *last;
T *space_end;
public:
//无参构造
My_vector():start(NULL),last(NULL),space_end(NULL){}
//有参构造(填入连续几个值进行初始化)
My_vector(int num, const T val)
{
start = new T[num];
memset(start, val, num);
space_end = (start+num);
last = (start+num-1);
}
//判满
bool full()
{
if(last == space_end)
{
return true;
}else
{
return false;
}
}
//判空
bool empty()
{
if(last == start)
{
return true;
}else
{
return false;
}
}
//目前存储元素的个数
int size()
{
return (last-start);
}
//当前已申请空间最大可存储元素
int capacity()
{
// cout << "------------" << space_end << "------------" << endl;
return (space_end - start);
}
//拷贝构造
My_vector(My_vector<T> &other)
{
start = new T[other.capacity()];
memcpy(start, other.start, other.size());
last = (start+other.size());
space_end = (start+other.capacity());
}
//容器地址来初始化
My_vector(T *start_other, T *end_other)
{
int size_temp = (end_other - start_other);
start = new T[size_temp];
memcpy(start, start_other, size_temp);
last = (start+size_temp);
space_end = (start+size_temp);
}
//拷贝赋值
My_vector &operator=(My_vector<T> &other)
{
delete []start;
start = NULL;
last = NULL;
space_end = NULL;
start = new T[other.capacity()];
memcpy(start, other.start, other.size());
last = (start+other.size());
space_end = (start+other.capacity());
}
//at
T &at(int side)
{
if(side >= this->size())
{
cout<< "error data, max is " << this->size()-1 << endl;
return start[side];
}else
{
return start[side];
}
}
//运算符重载
My_vector &operator==(My_vector<T> &other)
{
if(this->size() != other.size())
{
cout<< "不相等" <<endl;
}else
{
int flag = 0;
for(int i=0; i<other.size(); i++)
{
if(this->start[i] != other[i])
{
flag = 1;
break;
}
}
if(flag)
{
cout<< "不相等" << endl;
}else
{
cout<< "相等" << endl;;
}
}
}
My_vector &operator>(My_vector<T> &other)
{
int flag = 0;
for(int i=0; i<other.size(); i++)
{
if(this->start[i] > other[i])
{
flag = 1;
break;
}
}
if(flag)
{
cout<< "大于" << endl;
}else
{
cout<< "不大于" << endl;
}
}
My_vector &operator<(My_vector<T> &other)
{
int flag = 0;
for(int i=0; i<other.size(); i++)
{
if(this->start[i] < other[i])
{
flag = 1;
break;
}
}
if(flag)
{
cout<< "小于" << endl;
}else
{
cout<< "不小于" << endl;
}
}
T& operator[](int bit)
{
return start[bit];
}
//双倍扩容
void add_double_space()
{
int len = space_end - start;
if(0 != len)
{
T* temp = new T[2*len];
memcpy(temp, start, sizeof(T)*size());
int temp_size = (last - start);
int temp_capacity = (space_end - start);
cout << "------------" << temp_capacity << "------------" << endl;
delete []start;
start = NULL;
last = NULL;
space_end = NULL;
start = temp;
last = (start + temp_size);
space_end = (start + 2*temp_capacity);
}else if(len == 0)
{
start = new T;
space_end = start++;
last = start++;
}
}
//返回一个指向元素末尾的下一个位置的指示器
T *end() const
{
return last;
}
//返回一个指向元素末尾的下一个位置的指示器
T *begin() const
{
return start;
}
//返回当前vector最末一个元素的引用
T &back()
{
return *(last-1);
}
//返回当前vector起始元素的引用
T &front()
{
return *start;
}
//清楚Vector中的所有元素
void clear()
{
if(NULL != start)
{
memset(start, 0, size());
last = start;
}else
{
cout<< "没有数据空间,清楚失败" <<endl;
return;
}
}
//往vector中增加一个元素
void push(int bit, const T &val )
{
if(full())
{
add_double_space();
}
if(empty())
{
start = new T(val);
space_end = start++;
last = start++;
}
if((start+bit) < last)
{
int temp_size = last -(start+bit-1)+1;
T *temp = new T[temp_size];
memcpy(temp, (start+bit-1), temp_size);
start[bit] = val;
memcpy((start+bit), temp, temp_size);
last++;
delete []temp;
temp = NULL;
}else
{
*last++ = val;
}
}
//从vector中删除一个元素
void pop(int bit)
{
if(bit > capacity())
{
cout<< "error!!!,max bit is "<< capacity() <<endl;
return;
}
if(empty())
{
cout<< "vector为空,删除失败!!!" << endl;
return;
}
if(bit<size())
{
int temp_size = (last -(start+bit))+1;
T *temp = new T[temp_size];
memcpy(temp, (start+bit), temp_size);
memcpy((start+bit-1), temp, temp_size);
last--;
delete []temp;
temp = NULL;
}else
{
T *temp = end();
*temp = (T)0;
last -= 1;
}
}
//将区间[start, end)的元素赋到当前vector,或者赋num个值为val的元素到vector中.
//这个函数将会清除掉为vector赋值以前的内容.
void assign(T * temp_start, T * temp_end)
{
clear();
int temp_size = temp_end - temp_start+1;
if(capacity() >= temp_size)
{
memcpy(this->start, temp_start, temp_size);
last = start + temp_size-1;
}else
{
add_double_space();
memcpy(this->start, temp_start, temp_size);
last = start + temp_size-1;
}
}
void assign(int num, const T &val)
{
clear();
for(int i=0; i<num; i++)
{
push(i, val);
}
}
//添加值为val的元素到当前vector末尾
void push_back(const T &val)
{
push((last-start), val);
}
//函数删除当前vector最末的一个元素
void pop_back()
{
pop(size());
}
};
int main()
{
My_vector<int> v1;
cout<<"v1.size = "<<v1.size()<<endl; //0
cout<<"v1.capacity = "<<v1.capacity()<<endl; //0
cout<<"************************************************************\n";
for(int i=1; i<=20; i++)
{
v1.push_back(i);
cout<<"v1.size = "<<v1.size()<<" v1.capacity = "<<v1.capacity()<<endl;
cout << boolalpha << v1.full() << endl;
}
//输出当前vector中的内容
for(int i=0; i<v1.size(); i++)
{
//cout<<v1[i]<<" ";
cout<<v1.at(i)<<" ";
}
cout<<endl;
//调用尾删函数
v1.pop_back();
for(int i=0; i<v1.size(); i++)
{
//cout<<v1[i]<<" ";
cout<<v1.at(i)<<" ";
}
cout<<endl;
//将第一个元素的值改为100,最后一个值改为200
v1.front() = 100;
v1.back() = 200;
for(int i=0; i<v1.size(); i++)
{
//cout<<v1[i]<<" ";
cout<<v1.at(i)<<" ";
}
cout<<endl;
//使用迭代器来遍历数组
for(auto p=v1.begin(); p!=v1.end(); p++)
{
cout<<*p<<" ";
}
cout<<endl;
//清空vector
v1.clear();
cout<<"v1.size = "<<v1.size()<<endl; //0
cout<<"v1.capacity = "<<v1.capacity()<<endl; //?
for(int i=0; i<v1.capacity(); i++)
{
cout<<v1[i]<<" ";
//cout<<v1.at(i)<<" ";
}
cout<<endl;
//使用有参构造函数构造一个vector
My_vector<char> v2(5, 'A');
for(int i=0; i<v2.size(); i++)
{
cout<<v2[i]<<" ";
//cout<<v1.at(i)<<" ";
}
cout<<"v2.size = "<<v2.size()<<endl; //5
cout<<"v2.capacity = "<<v2.capacity()<<endl; //5
v2.push_back('G');
cout<<"v2.capacity = "<<v2.capacity()<<endl; //10
//使用别的容器给新容器初始化
int arr[] = {3,8,4,6,2,9,1,0,7};
My_vector<int> v3(arr, arr+5);
for(int i=0; i<v3.size(); i++)
{
cout<<v3[i]<<" ";
//cout<<v1.at(i)<<" ";
}
return 0;
}