(本资料qq讨论群112133686)
result = (a>b)a : b; //
cout <<“result
提示:模板并非通常意义上可直接使用的函数或类,仅仅是对一族函数或类的描述,是参数化的函数和类。模板是一种使用无类型参数来产生一族函数或类的机制。
using namespace std;
template <class T>
class Operate{ //
}
else{ a/b;
} } };
int main(){
int A,B,C,D,E,x,y,z; //
13.2
#include <iomanip>
using namespace std;
//
output(matrix1,”matrix1″,3,6);
output(matrix2,”matrix2″,3,4);
output(result,s1,6,4);
system(“pause”);
return 0;
}
template <typename T1,typename T2>void multi(T1 *mat1,T2 *mat2,T2 *result,int a,int b,int c){
//
for (k=0;k<b;k++) //
result = a-b;
return (result);
}
template <class T>
void display (T a, T b) { //
double l=30.23, m=40;
display(l,m);
cout << “a+b Add():” << Add(l,m) << endl; //
system(“pause”);
return 0;
}
【案例分析】
bool dblX=1,dblY=0;
cout<<“
return 0;
}
template<class T>
T Greater(T x,T y)
{
return x<y:y;
}
template<> bool Greater(bool x,bool y) //
using namespace std;
template<class T>
const T&max(const T&x,const T&y) //
}
const int&max(const int&x,const int&y) //
}
void main()
{
int i=50;
char c=’A’;
double f=100.05;
cout<<max(i,i)<<endl; //
}
【案例分析】
#include <string.h>
#include <malloc.h>
#include <process.h>
#define GENERIC
template <typename T> //
return 0;
memcpy(item_temp, item1,item_size); //
}
void main()
{
int i=0,j=1;
swap(&i, &j, sizeof(int)); //
swap(&s, &d, sizeof(char)); //
#include<iostream>
using namespace std;
const int size=100; //
T pop ();
int push (T);
int getcount();
int display();
private: T IArray [size]; //
{
IArray[i]=0;
}
count=0;
}
template <typename T> //
{
IArray [count++]=rhs;
return 0;
}
template <typename T>
int IStack<T>::display() //
}
int main()
{
IStack<int> istack;
int i, n; //n
cin>>n;
cout<<“
istack.display();
system(“pause”);
return 0;
}
【案例分析】
判断参数为字符串类型就输出字符串
【案例描述】
若有两个或多个类,其功能是相同的,仅仅是数据类型不同,如声明成几个类效率显然不高,这时就可以用类模板提高效
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!
(本资料qq讨论群112133686)
result = (a>b)a : b; //
cout <<“result
提示:模板并非通常意义上可直接使用的函数或类,仅仅是对一族函数或类的描述,是参数化的函数和类。模板是一种使用无类型参数来产生一族函数或类的机制。
using namespace std; template <class T> class Operate{ //
} else{ a/b; } } }; int main(){ int A,B,C,D,E,x,y,z; //
13.2
#include <iomanip> using namespace std; // output(matrix1,”matrix1″,3,6); output(matrix2,”matrix2″,3,4); output(result,s1,6,4); system(“pause”); return 0; } template <typename T1,typename T2>void multi(T1 *mat1,T2 *mat2,T2 *result,int a,int b,int c){ //
for (k=0;k<b;k++) //
result = a-b; return (result); } template <class T> void display (T a, T b) { //
double l=30.23, m=40; display(l,m); cout << “a+b Add():” << Add(l,m) << endl; //
system(“pause”); return 0; }
bool dblX=1,dblY=0; cout<<“
return 0; } template<class T> T Greater(T x,T y) { return x<y:y; } template<> bool Greater(bool x,bool y) //
using namespace std; template<class T> const T&max(const T&x,const T&y) //
} const int&max(const int&x,const int&y) //
} void main() { int i=50; char c=’A’; double f=100.05; cout<<max(i,i)<<endl; //
}
#include <string.h> #include <malloc.h> #include <process.h> #define GENERIC template <typename T> //
return 0; memcpy(item_temp, item1,item_size); //
} void main() { int i=0,j=1; swap(&i, &j, sizeof(int)); //
swap(&s, &d, sizeof(char)); //
#include<iostream> using namespace std; const int size=100; //
T pop (); int push (T); int getcount(); int display(); private: T IArray [size]; //
{ IArray[i]=0; } count=0; } template <typename T> //
{ IArray [count++]=rhs; return 0; } template <typename T> int IStack<T>::display() //
} int main() { IStack<int> istack; int i, n; //n cin>>n; cout<<“ istack.display(); system(“pause”); return 0; }
判断参数为字符串类型就输出字符串 若有两个或多个类,其功能是相同的,仅仅是数据类型不同,如声明成几个类效率显然不高,这时就可以用类模板提高效 声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!
【案例分析】
【案例分析】
【案例分析】
【案例描述】