In C or C + +, we've learned how to implement a static order table, so why introduce a template class to implement a static order table? First, let's see, before we introduce a template, we define a static sequential table in C + +:
typedef int DATATYPE; At this point I define the Int type class seqlist{datatype* _array; size_t _size; size_t _capacity;}
If we want to define a char or other type, we can use typedef , which is where the advantages of using typedef are. In addition, it is advantageous to unify the management program code, increase the maintainability of the code.
However, sometimes we will not encounter such a problem:
void Test () {seqlist S1; At this time S1 I want to define a char type seqlist s2; At this point S2 I want to define a non-char type}
At this time, if we use typedef in exchange for change, will inevitably make code confusion or error. If you define a class to implement another type of static order table, the code is very similar in two places and is not conducive to maintenance. Therefore, we introduce the template, the template class instantiation can easily solve the problem.
The code is as follows:
#include <iostream>using namespace std; #include <string> #include <assert.h>template <class T>class SeqList{private: T* _array; size_t _size; size_t _capacity; public: seqlist () :_array (NULL) , _size (0) , _capacity (0) {} seqlist (const seqlist<t>& s) { _array = new T[s._size]; for (size_t i = 0; i < s._size ; i++) { _array[i] = s._array[i]; } _size = s._size; _capacity = s._size; } seqlist<t>& operator= (SeqList <t> s) { if (&s != this) { swap (_array, s._array); swap (_size, s._size); swap (_capacity, s._capacity); } return *this; } ~ Seqlist () { if (_array) { delete[] _array; } } //Expansion void _checkcapacity (size_t n) { if (n > _capacity) { _capacity = n > 2 * _capacity + 3 ? n : 2 * _capacity + 3; /*_array = (t*) realloc (_array, sizeof (T) * _capacity); */ // Not OK! ReAlloc The constructor initialization object is not called for a custom type, the random value crashes. T* tmp = new T[_capacity]; if (_array) { /*memcpy (tmp, _array, sizeof (T) * _size);*/ //can't be done! Re-opening space copy destruction for long strings two crashes for (size_t i = 0; i < _size; i++) { tmp[i] = _array[i]; } } delete[] _array; _array = tmp; } } //Printing void printseqlist () { for (size_t i = 0; i < _size; i++) { cout << _array[i] << " "; } cout << endl; } //Tail Plug Void pushback (const t& x) { _checkcapacity (_size + 1); _array[_size++ ] = x; } //operator[] t& operator[] (Size_t index) { ASSERT (Index < _size); return _array[index]; } //Request Space void reserve (size_t n ) { _checkcapacity (n); }};void test () { seqlist<int> s1; s1. Pushback (1); s1. Pushback (2); s1. Pushback (3); s1. Pushback (4); s1. Pushback (5); s1. Printseqlist (); seqlist<string> s2; s2. Pushback ("xxx"); s2. Pushback ("xxx"); s2. Pushback ("xxx"); s2. Pushback ("xxx"); s2. Pushback ("xxx"); s2. Printseqlist (); &NBSP;SEQLIST<STRING>&NBSP;S3 (S2); s3[0] = "yyy"; s3. Printseqlist (); }int main () { test (); system ("pause"); return 0;}
Here, we focus on the reserve () function, which implements the request for a capacity. We look at cplusplus.com to find the reserve (size_t N) in the vector (sequential table). n indicates how much space to open up.
Other functions before the blog implementation, we do not repeat the implementation here.
This article is from the "C language 100-200 Prime" blog, please be sure to keep this source http://10740184.blog.51cto.com/10730184/1750309
"C + +" template class implements ordinary class static order table