Template implementation Dynamic Sequential table

Source: Internet
Author: User

When you define a class template, member functions can be defined inside the class or outside the class template.

in this case, if there is a type parameter in the member function, you need to note:

(1) to make a template declaration before the member function definition.

(2) Add "class name < type parameter >" before the member function name


#define  _CRT_SECURE_NO_WARNINGS 1#include<iostream>using namespace std; #include < assert.h>//  defines the template class    (type parameter is T) Template<typename  t>class seqlist{public: Seqlist (); Seqlist (const seqlist & slist); Seqlist& operator= (const seqlist& slist);//function Implementation void checkcapacity ();   // Check capacity and expand Void pushback (const t & x);      //Tail plug Void popback ( );                  // Tail Void pushfront (const t & x);     //Head plug Void popfront ();                 //Tail Delete         int find (const t & x);           //find Xvoid print ();                     //Print List void  Insert (size_t pos, const t& x);     //inserts xvoid erase after a position (size _t pos);                  //Delete data from a location Int remove (const t & x);                 //Delete x (first look and then delete) ~seqlist () {if  (_array != null) {delete[] _array;_size = 0;_capicity = 0;}} private:t* _array;size_t  _size;size_t _capicity;}; template<typename t>      //Template declaration seqlist<t>::seqlist ()       //defines the constructor     :_array (NULL) in the class template body     , _ Size (0)     , _capicity (0) {}template<typename t>          //Template declaration seqlist<t>::seqlist (const seqlist & slist)         //defining a copy constructor     :_array (New t[slist._size]) outside the class template body     , _size (slist._size)     , _capicity (sList._capicity) { memcpy (_array, slist._array, sizeof (T) *_size);} template<typename t>        //Template Declaration seqlist<t>&  seqlist<t>:: operator= (const seqlist<t>& slist)  //assignment operator overloading {if  ( &slist != this) {T *tmp = new t[slist._size];d Elete[] _array;_array  = tmp;_size = slist._size;_capicity = slist._capicity;memcpy (_array, sList . _array, sizeof (T) *_size);} Return *this;} template<typename t>                     //Template declaration void seqlist<t>::checkcapacity () {if  (_size >= _capicity) {_ capicity = 2 * _capicity + 5;_array =  (T *) realloc (_array, _ capicity*sizeof  (T));}} Template<typename t>void seqlist<t>::P rint () {for  (int i = 0; i  < _size; ++i) {cout << _array[i] <<  " ";} Cout << endl;} Template<typename t>void seqlist<t&gt::P ushback (const t & x) { Checkcapacity (); _array[_size++] = x;} Template<typename t>void seqlist<t&gt::P opback () {if  (_size == 0) {cout  <<  "this seqlist is empty !"  << endl;return;} Else{_array[--_size] = null;}} Template<typename t>void seqlist<t&gt::P ushfront (const t & x) {if  ( _SIZE&NBSP;==&NBSP;0){pushback (x); return;} Else{checkcapacity ();int key = _size;while  (key) {_array[key--] = _array[key &NBSP;-&NBSP;1];} _array[0] = x;_size++;}} Template<typename t>void seqlist<t&gt::P Opfront () {if  (_size == 0 | |  _size == 1) {popback ();} else{for  (int i = 0; i < _size - 1; i++) {_array[i] = &NBSP;_ARRAY[I&NBSP;+&NBSP;1];} --_size;}} Template<typename t>void seqlist<t>::insert (size_t pos, const t&  x) {assert (pos<_size); Checkcapacity ();if  (pos == _size - 1) {pushback (x); return;} else{for  (int i = _size; i > pos; --i) {_array[i] = _array [I - 1];} _array[pos] = x;_size++;}} Template<typename t>int seqlist<t>::find (const t & x) {assert (_array  != null);for  (INT&Nbsp;i = 0; i < _size; i++) {if  (_array[i] == x) return i;} Return -1;} Template<typename t>void seqlist<t>::erase (Size_t pos) {assert (_array !=  NULL), assert (Pos < _size);if  (pos == _size - 1) {popback (); return;} if  (pos == 0) {Popfront (); return;} for  (int i = pos; i < _size - 1; i++) {_array[i] = &NBSP;_ARRAY[I&NBSP;+&NBSP;1];} --_size;} Template<typename t>int  seqlist<t>::remove (const t & x) {assert ( _array); Int pos = find (x);if  (pos != -1) {Erase (POS); return 1;} Else{return -1;}} Test Case Void test1 () {seqlist<int> list1;list1. Pushback (1); List1. Pushback (2); List1. Pushback (3); List1. Pushback (4); List1. Pushback (5); List1. Print (); Seqlist<int> list2;list2. Pushback (0); List2. Pushback (9); List2. Pushback(8); List2. Pushback (7); List2. Pushback (6); List2. Print (); List1 = list2;list1. Print (); Seqlist<int> list3 (List1); List3. Print ();} Void test2 () {seqlist<int>  list1;//list1. Pushfront (0);//list1. Print (); List1. Pushback (1); List1. Pushback (2); List1. Pushback (3); List1. Pushback (4); List1. Pushback (5); List1. Print ();//list1. Popfront ();//list1. Print ();/*list1. Insert (2, 0); List1. Print ();*///cout << "The number is now: _array[" << list1. Find (5)  << "]" << endl;//list1. Erase (2); Int ret = list1. Remove (7);if  (ret == -1) {cout <<  "not exit !"  << endl;} Else{list1. Print ();}} Int main () {Test1 ();//test2 (); System ("pause"); return 0;}


This article from "Yan Anyang" blog, declined reproduced!

Template implementation Dynamic Sequential table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.