To construct an n-weft grid As an example, this paper describes the template recursion.
The first is a simple realization of a mesh that implements the instantiation of the specified length grid and can be transformed freely in different sizes of grid classes (through the cast_ctr of the template nesting)
(Use of technology, non-type parameter template, template nesting, class template special case, template friend function)
#include <cassert> #include <iostream> using namespace std;
Template <typename T,int length> class grid; Template <typename t,int length> ostream& operator<< (ostream& os, const grid<t, LENGTH>& g
d) {os<< "[[Grid with Size:" <<gd.getsize () << "]";
for (int i=0;i<gd.getsize () -1;i++) {os<<gd[i]<< "\ t";
} os<<gd[gd.getsize () -1]<< "]" <<endl;
return OS;
Template <typename T,int length> class grid{Public:grid (): Size (0), Mcells (new T[length]) {};
Grid (const grid<t,length>& GD): Mcells (new T[length]), size (gd.size) {for (int i=0;i<gd.size;i++) {
Mcells[i]=gd.mcells[i]; }//enough, any modified T or length will be handle by this nested template template<typename E, int newlength>//required e->t Natur Ally Grid (const grid<e,newlength>& GD): Mcells (New T[length)), size (0) {int newsize=length>gd.geTsize () gd.getsize (): LENGTH;
for (int i=0;i<newsize;i++) {* (mcells+i) =gd[i];
} size=newsize;
} grid<t,length>& operator= (const grid<t,length>& GD) {size=gd.getsize ();
t* su1= Mcells;
t* su2= Gd.mcells;
while (su1!=mcells+gd.size-1) {*su1++=*su2++; } template<typename E, int newlength>//required e->t naturally grid<t,length>& Opera
tor= (const grid<e,newlength>& GD) {int newsize=length>gd.getsize () gd.getsize (): LENGTH;
for (int i=0;i<newsize;i++) {* (mcells+i) =gd[i];
} size=newsize;
} inline int GetSize () const {return size;}
Virtual ~grid () {delete mcells;mcells=nullptr;};
t& operator[] (int index) {if (index>=size) {resize (index+1);
return * (Mcells+index); Const t& operator[] (int index) const{Assert (Index<getsize ());
return * (Mcells+index);
} void Resize (int newsize,const t& def=t ()) {assert (newsize<=length);
if (newsize<=size) {size=newsize;
else {int i=size;
for (; i<=newsize-1;i++) {* (mcells+i) =def;
} size=newsize;
} friend ostream& operator<< <T,LENGTH> (ostream& os, const grid<t, length>& GD);
private:t* Mcells;
int size; };
The test code is as follows:
#define _test_grid_ 1
#if _test_grid_
#include "grid.h"
#include <iostream>
Include <ctime>
#include <cstdio>
using namespace std;
int main () {
//grid instantiation
grid<int,20> A;
Srand ((int) time (NULL));
for (int i=0;i<20;i++) {
a[i]=rand ()%30;
}
Different sizes, different types of grids copy each other
grid<double,40>b (a);
Grid<double,10>c (a);
cout<<a<<b<<c;
By using a weft mesh, the caller realizes the template recursion (actually the instance recursion)
grid<int,40> Onegrid;
Grid<grid<int, 40>, 40> Twogrid;
unsigned long lct=time (NULL);
for (int k=0;k<40;k++) {
srand (unsigned) (Lct-clock ());
for (int i=0;i<20;i++) {
twogrid[k][i]=rand ()%30
}}
cout<<twogrid;
}
#endif
This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/