封装Vector
#include <iostream>
using namespace std;
#define MAXSIZE 10
template <typename T>
class Myvector
{
private:
T *first;
T *last;
T *end;
public:
//构造
Myvector(int size = 2):
first(new T[size]){
last= first+1;
end =first+size;
cout << "no have" << endl;
}
//析构函数
~Myvector();
//拷贝构造
Myvector(const Myvector &other);
//拷贝赋值
// Myvector &operator =(const Myvector &other);
//at()
T my_at(int index);
//判满
bool my_full();
//判空
bool my_empty();
//头元素
T my_front();
//最后一个元素
T my_back();
//容量大小
int my_size();
//清空
void my_clear();
//二倍扩容
int my_expand();
//尾插 push_back
void push_back(T e);
//尾删 pop_back
void pop_back();
void show(){
int i = 0;
for(;i < my_size();i++){
cout << first[i] << " ";
}
cout << endl;
}
};
int main()
{
Myvector <int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
v1.push_back(6);
v1.show();
//头元素
// try{
cout << "first "<< v1.my_front() << endl;
// }catch(string e){
// cout << "空" << endl;
//}
//尾元素
//try{
cout << "last "<< v1.my_back() << endl;
//}catch(string e){
// cout << "空" << endl;
// }
//大小
cout << "size " << v1.my_size()<<endl;
//at
//try{
cout << "3data " << v1.my_at(3) << endl;
//}catch(string e){
// cout << "越界" << endl;
//}
//尾删
v1.pop_back();
v1.pop_back();
v1.show();
cout<< v1.my_expand()<< endl;
Myvector <int> v2(v1);
v2.show();
return 0;
}
//析构
template<typename T>
Myvector<T>::~Myvector(){
delete [] first;
first = nullptr;
last = nullptr;
end = nullptr;
cout << "destroy" << endl;
}
//拷贝构造
template<typename T>
Myvector<T>::Myvector(const Myvector &other){
int len = other.last-other.first;
int size = other.end-other.first;
this->first = new T[size];
memcpy(this->first, other.first, len*sizeof(T));
this->last = this->first + len;
this->end = this->first + size;
}
拷贝赋值
//template<typename T>
//Myvector<T> &Myvector<T>::operator =(const Myvector &other){
// if(this != other){
// this->first = new T[sizeof(other)];
// memcpy(this->first, other.first, sizeof(other));
// this->last = this->first + my_size();
// this->end = this->first + my_expand();
// }
// cout << "拷贝赋值" << endl;
// return *this;
//}
//at
template<typename T>
T Myvector<T>::my_at(int index){
if(index < 0 || index > my_size()){
throw string("越界");
}
return first[index];
}
//判满
template<typename T>
bool Myvector<T>::my_full(){
return !(end - last);
}
//判空
template<typename T>
bool Myvector<T>::my_empty(){
return !(last - first);
}
//首元素
template<typename T>
T Myvector<T>::my_front(){
if(true == my_empty()){
throw string("空");
}
return *first;
}
//尾元素
template<typename T>
T Myvector<T>::my_back(){
if(true == my_empty()){
throw string("空");
}
return *(--last);
}
//大小
template<typename T>
int Myvector<T>::my_size(){
return (last-first);
}
//清空
template<typename T>
void Myvector<T>::my_clear(){
delete [] first;
first = nullptr;
last = nullptr;
end = nullptr;
}
//二倍扩容
template<typename T>
int Myvector<T>::my_expand(){
int size = this->end - this->first;
T *temp = new T[2*size];
memcpy(temp, this->first,size*sizeof(T));
delete [] first;
first = temp;
last = first+size;
end = first + 2*size;
return 2*size;
}
//尾插
template<typename T>
void Myvector<T>::push_back(T e){
if(this->my_full()){
this->my_expand();
}
*last = e;
last++;
}
//尾删
template<typename T>
void Myvector<T>::pop_back(){
--last;
}