#include <cstdio>#include<algorithm>#include<cstdio>#include<cstring>#include<iostream>#include<vector>#include<stack>#include<queue>#include<string>using namespacestd;/** "1" templates programming, Template <typename t> declaration template*/Template<typename t>t Findmax (t arr[],intLen) {T Val=arr[0]; for(intI=1; i<len;i++) { if(Arr[i]>val) val=Arr[i]; } returnVal;}/** "2" class template*/Template<intN,typename t>T*Create () {T*str=NewT[n]; returnstr;}///Call Char *p=create<100,char>();///Example of "3" stack templateTemplate <typename t>classstack{ Public: Stack (intmaxsize) {M_maxsize=maxsize; M_buffer=NewT[maxsize]; M_size=0; } ~Stack () {Delete[] m_buffer; } BOOLPushConstt&value) { if(m_size>=m_maxsize)return false; M_buffer[m_size]=value; M_size++; return true; } t POPs () {T last=m_buffer[m_size-1]; M_size--; returnLast ; } Constt&Top () {returnm_buffer[m_size-1]; } int_size () {returnm_size; }Private: T*M_buffer; intm_size; intm_maxsize;};intMain () {///"1"Puts"1: Template programming"); intarr[]={1,2,9,6,4}; Double_arr[]={1.5,4.3,5.5,9.8,0.6}; printf ("%d%.2f\n", Findmax (arr,5), Findmax (_arr,5)); printf ("%d \ n",findmax<int> (arr,5));///<int>instantiate a template return 0;}
C + + Template programming