myVector
#include <iostream>
using namespace std;
template <typename T>
class myVector
{
private:
T* first;
T* last;
T* end;
public:
myVector():first(nullptr), last(nullptr), end(nullptr) {}
myVector(int size)
{
first = new T[size];
last = first;
end = first + size;
}
//析构函数
~myVector()
{
delete []first;
first = nullptr;
last = nullptr;
end = nullptr;
}
//拷贝构造
myVector(const myVector &other)
{
int size = other.last-other.frist;
first = new T[size];
last = first + size - 1;
end = first + size;
for (int i = 0; i < size; i++)
{
first[i] = other.first[i];
}
}
//拷贝赋值
myVector &operator=(const myVector<T> &other)
{
if(this != &other)
{
int size = other.last-other.frist;
first = new T[size];
last = first + size - 1;
end = first + size;
for (int i = 0; i < size; i++)
{
first[i] = other.first[i];
}
}
return *this;
}
//empty
bool empty()
{
return first == last;
}
//full
bool full()
{
return last == end;
}
//front
T& front()
{
return *first;
}
//back
T& back()
{
return *(last-1);
}
//size
int size()
{
return last - first;
}
//at
T at(int pos)
{
return *(first+pos);
}
//clear
void clear()
{
last = first;
end = first + 1;
}
//扩容
void expand() {
int size = this->last - this->first;
T* new_p = new T[size * 2];
memccpy(new_p, first, size);
delete[] first;
first = new_p;
last = first + size;
end = first + size * 2;
}
//push_back()
void push_back(T v)
{
if(!full())
{
*last = v;
last++;
}
}
//pop_back
void pop_back()
{
if(!empty())
{
--last;
}
}
};
int main()
{
myVector<int> d1;
for(int i = 0;i<8;i++)
{
d1.push_back(i);
}
cout<<d1.at(3)<<endl;
d1.front() = 1314;
d1.back() = 520;
for(int i = 0;i<8;i++)
{
d1.push_back(i);
}
d1.pop_back();
for(int i = 0;i<8;i++)
{
d1.push_back(i);
}
cout<<"是否为满 = "<<d1.full()<<endl;
cout<<"是否为空 = "<<d1.empty()<<endl;
d1.clear();
cout<<"是否为满 = "<<d1.full()<<endl;
cout<<"是否为空 = "<<d1.empty()<<endl;
return 0;
}