#include <iostream>
template<class T> class Vector {
private:
size_t len;
T *items;
public:
Vector(size_t size = 10): len(size), items(new T[len]) {}
~Vector() {delete[] items;};
void resize(size_t size) {
if(size <= len) return;
T *tmp = new T[size];
for(size_t i = 0; i < len; i++) {
tmp[i] = items[i];
}
delete[] items;
items = tmp;
len = size;
}
size_t size() {return len;}
T &operator[](size_t i) {
if(i > len) resize(i + 1);
return items[i];
}
const T &operator[](size_t i) const {return items[i];}
};
int main() {
Vector<std::string> a(10);
a[0] = "哈哈";
a[1] = "哈哈1";
a[2] = "哈哈2";
a[3] = "哈哈3";
a[4] = "哈哈4";
for(size_t i = 0; i < a.size(); i++) {
std::cout << a[i];
}
std::cout << std::endl;
}
|