本文共 1522 字,大约阅读时间需要 5 分钟。
#include using namespace std;template class Seqlist { public: Seqlist() :_data(NULL) , _size(0) , _capacity(0) { CheckCapacity(); } ~Seqlist() { if (_data != NULL) { delete[] _data; } } Seqlist(const Seqlist & s) { _data = new T[s._size * sizeof(T)]; int i = 0; for (i = 0; i < s._size; i++) { _data[i] = s._data[i]; } _size = s._size; _capacity = s._size; } void PushBack(const T& d); void PopBack(); void PushFront(const T& d); void PopFont(); void Print() { int i = 0; for (i = 0; i < _size; i++) { cout << _data[i] << ""; } cout << endl; } public: void CheckCapacity() { if (_size == _capacity) { T* tmp = new T[2 * _capacity + 3]; /*memcpy(tmp,_data, _size*sizeof(T));*/ int i= 0; for (i = 0; i < _size; i++) { tmp[i] = _data[i]; } delete[] _data; _data = tmp; _capacity = 2 * _capacity + 3; } } private: T* _data; int _size; int _capacity; }; template void Seqlist ::PushBack(const T& d) { CheckCapacity(); _data[_size] = d; _size++; } template void Seqlist ::PopBack() { } template void Seqlist ::PushFront(const T& d) { } template void Seqlist ::PopFont() { }int main(){ Seqlist s1; s1.PushBack(1111111); s1.PushBack(2222222); s1.PushBack(3333333); s1.PushBack(4444444); s1.Print(); //cout << sizeof(string) << endl; getchar(); return 0;}
转载于:https://blog.51cto.com/lk123456/1754104