#include <iostream>
using namespace std;
template <typename T>
class Myvector
{
private:
T * first;
T * last;
T * end;
public:
Myvector() {
first = new T;
last = first+1;
end = first;
}
// 构造函数
Myvector(T f,T l,T e):first(new T(f)),last(new T(l)),end(new T(e)) {}
// 析构函数
~Myvector()
{
delete first;
first = 0;
last = 0;
end = 0;
cout << "析构函数" << endl;
}
// 拷贝构造
Myvector(Myvector &other):first(new T(*(other.first))),last(new T(*(other.last))),end(new T(*(other.end)))
{}
// 拷贝赋值
Myvector &operator=(const Myvector &other)
{
if(this != &other)
{
*(this->first) = *(other.first);
*(this->last) = *(other.last);
*(this->end) = *(other.end);
}
}
// at()
T& at(int pos)
{
return first[pos];
}
// empty()
bool empty()
{
if(first == end)
return true;
else
return false;
}
// full()
bool full()
{
if(last == end)
return true;
else
return false;
}
// front()
T front()
{
if(!empty())
{
return *first;
}
}
// back()
T back()
{
if(!empty())
{
return *end;
}
}
// capacity()
T capacity()
{
return last - first;
}
// size()
int size()
{
return end - first;
}
// clear()
void clear()
{
end = first;
cout << "clear success" << endl;
}
// expand() 二倍扩容函数
void expand()
{
int size = last - first;
T* temp = new T[size*2];
memcpy(temp,first,sizeof (T)*size);
delete []first;
first = temp;
end = first + size;
last = first + 2*size;
}
// push_back()
void push_back(const T &x)
{
if (full ())
{
expand ();
}
*end = x;
end = end + 1;
}
// pop_back()
void pop_back()
{
if(!empty())
{
end--;
}
}
};
int main()
{
Myvector<int> v1;
// size()
cout << "v1.size = " << v1.size() << endl;
cout << "v1.capacity = " << v1.capacity() << endl;
// 判空
if(v1.empty()){
cout << "empty" << endl;
}else{
cout << "not empty" << endl;
}
// 判满
if(v1.full()){
cout << "full" << endl;
}else{
cout << "not full" << endl;
}
for(int i=1;i<=10;i++)
{
v1.push_back(i);
cout << "v1.size = " << v1.size() << endl;
cout << "v1.capacity = " << v1.capacity() << endl;
}
//遍历
cout << "--------------------------------" << endl;
for(unsigned long long i=0;i<v1.size();i++)
{
cout << v1.at(i) << " ";
}
cout << endl;
cout << "--------------------------------" << endl;
if(v1.empty()){
cout << "empty" << endl;
}else{
cout << "not empty" << endl;
}
v1.pop_back();
cout << "v1.size = " << v1.size() << endl;
cout << "v1.capacity = " << v1.capacity() << endl;
cout << "v1.front() = " << v1.front() << endl;
cout << "v1.back() = " <<v1.back() << endl;
//遍历
cout << "--------------------------------" << endl;
for(unsigned long long i=0;i<v1.size();i++)
{
cout << v1.at(i) << " ";
}
cout << endl;
cout << "--------------------------------" << endl;
v1.push_back(11);
//遍历
cout << "--------------------------------" << endl;
for(unsigned long long i=0;i<v1.size();i++)
{
cout << v1.at(i) << " ";
}
cout << endl;
cout << "--------------------------------" << endl;
v1.clear();
cout << "v1.size = " << v1.size() << endl;
cout << "v1.capacity = " << v1.capacity() << endl;
if(v1.empty()){
cout << "empty" << endl;
}else{
cout << "not empty" << endl;
}
return 0;
}
04/25课后作业(C++)
于 2023-04-25 21:46:26 首次发布