#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<cctype>
#include<fstream>
using namespace std;
template<typename T>
class Vector
{
private:
T* p;
int size;
public:
Vector() {
size = 0;
}
Vector(int psize) :size(psize) {
delete[]p;
p = new T[psize];
}
Vector(const Vector& tr) {
if (tr.size == 0) {
delete[]p;
p = NULL;
return;
}
delete[]p;
p = new T[tr.size];
memcpy(p, tr.p, sizeof(T) * tr.size);
size = tr.size;
}
~Vector() {}
T& operator[](int i) {
return p[i];
}
Vector& operator=(const Vector& tr) {
if (tr.size == 0) {
delete[]p;
p = NULL;
return *this;
}
delete[]p;
p = new T[tr.size];
memcpy(p, tr.p, sizeof(T) * tr.size);
return *this;
}
void push_back(T ele) {
if (p == NULL) {
p = new T[1];
p[size++] = ele;
}
else {
T* ptr = new T[size + 1];
memcpy(ptr, p, sizeof(T) * (size));
ptr[size++] = ele;
delete[]p;
p = ptr;
}
}
int getsize() {
return size;
}
};
int main()
{
Vector<int>v1,v2;
cout << v1.getsize() << endl;
for (int i = 0; i < 5; i++) {
v1.push_back(i);
}
v2 = v1;
for (int i = 0; i < 5; i++) {
cout << v2[i]<<" ";
}
cout << endl;
Vector<string>vv(100);
vv[10] = "hello";
cout << vv[10];
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-824.html