c++引入模板是为了更好的代码复用,模板这边分为两个大块.
1.模板函数
2.模板类
我们今天来介绍模板类的应用—顺序表和链表(单链表为例)
//模板的顺序表 template<class T> class SeqList { public: SeqList() :_array(NULL) , _size(0) , _capacity(0) {} ~SeqList() { if (_array) { delete[] _array; //free(_array); } } SeqList(const SeqList<T>& s) { _array = new T[_capacity]; //_array = (T*)malloc(sizeof(T)*s._capacity); /*memcpy(_array, s._array, s._capacity);*/ //只是将数组的首元素 赋值过去,并没有将所有值赋值过去。 for (int i = 0; i < s._size; i++) { _array[i] = s._array[i]; } _size = s._size; _capacity = s._capacity; } SeqList<T>& operator=(const SeqList<T>& s) { if (this != &s) { _array = new T[_capacity]; /*memcpy(_array, s._array, s._capacity);*/ //只是将数组的首元素 赋值过去,并没有将所有值赋值过去。 for (int i = 0; i < s._size; i++) { _array[i] = s._array[i]; } _size = s._size; _capacity = s._capacity; } return *this; } void PushBack(const T& x) { _CheckCapacity(); _array[_size++] = x; } void PopBack() { if (_size > 0) { _size--; } else { cout << "NO DATA!" << endl; } } void Print() { for (int i = 0; i < _size; i++) { cout << _array[i] << "->"; } cout << endl; } protected: void _CheckCapacity() { if (_size >= _capacity) { /*_capacity = _capacity * 2 + 1; _array = (T*)realloc(_array, sizeof(T)*_capacity);*/ _capacity = _capacity * 2 + 1; T* tmp = new T[_capacity]; //考虑到string类型,用malloc\realloc不会调用构造函数 if (_array) { memcpy(tmp, _array, _size); } delete[] _array; _array = tmp; } } private: T* _array; int _size; int _capacity; }; void Test1() { SeqList<int> s1; s1.PushBack(1); s1.PushBack(2); s1.PushBack(3); s1.PushBack(4); SeqList<int> s2(s1); s2.PushBack(5); s2.PushBack(6); s2.PushBack(7); s2.PushBack(8); s2.PushBack(9); s1.Print(); s2.Print(); } void Test2() { SeqList<int> s1; s1.PushBack(1); s1.PushBack(2); s1.PushBack(3); s1.PushBack(4); s1.Print(); s1.PopBack(); s1.PopBack(); s1.PopBack(); s1.PopBack(); s1.PopBack(); } void Test3() { SeqList<int> s1; s1.PushBack(1); s1.PushBack(2); s1.PushBack(3); s1.PushBack(4); SeqList<int> s2; s2 = s1; s1.Print(); s2.Print(); }
template<class T> //模板定义友元的时候是需要在声明的 class LinkList; template<class T> class LinkListNode { friend class LinkList<T>; public: LinkListNode(const T& x) :_data(x) , _next(NULL) {} private: T _data; LinkListNode<T>* _next; }; template<class T> class LinkList { public: LinkList() :_head(NULL) {} ~LinkList() { if (_head) { delete _head; } } LinkList(const LinkList<T>& l) //一个一个结点赋值 { LinkListNode<T>* cur = l._head; while (cur) { if (_head == NULL) { _head = new LinkListNode<T>(cur->_data); } else { LinkListNode<T>* tmp = new LinkListNode<T>(cur->_data); LinkListNode<T>* cur = _head; while (cur->_next) { cur = cur->_next; } cur->_next = tmp; tmp->_next = NULL; } cur = cur->_next; } } void PushBack(const T& x) { if (_head == NULL) { _head = new LinkListNode<T>(x); } else { LinkListNode<T>* tmp = new LinkListNode<T>(x); LinkListNode<T>* cur = _head; while (cur->_next) { cur = cur->_next; } cur->_next = tmp; tmp = NULL; } } void print() { LinkListNode<T>* cur = _head; while (cur) { cout << cur->_data << "->"; cur = cur->_next; } } private: LinkListNode<T>* _head; }; void Test3() { LinkList<int> l1; l1.PushBack(1); l1.PushBack(2); l1.PushBack(3); l1.PushBack(4); l1.print(); } void Test4() { LinkList<int> l1; l1.PushBack(1); l1.PushBack(2); l1.PushBack(3); l1.PushBack(4); LinkList<int> l2(l1); l1.print(); cout << endl; l2.print(); }
以上是本人在学习时候的代码,供你参考。
时间: 2024-11-02 13:26:18