模板 是指创建 泛型类或函数的蓝图或公式 ,我们可以使用模板来定义函数和类。模板是 泛型编程 的基础( 泛型编程 即以一种独立于任何特定类型的方式编写代码)。
【 1. 函数模板 】
函数模板的一般形式如下:
type 是函数所使用的数据类型的占位符名称,这个名称可以在函数定义中使用。 type 一般为 T , T表示通用数据类型 不报错
template < typename type > ReturnType FunctionName ( parameter list)
{
}
# include <iostream>
# include <string>
using namespace std;
template < typename T > inline T const & Max ( T const & a, T const & b)
{
return a < b ? b: a;
}
int main ( )
{
int i = 39 ;
int j = 20 ;
cout << "Max(i, j): " << Max ( i, j) << endl;
double f1 = 13.5 ;
double f2 = 20.7 ;
cout << "Max(f1, f2): " << Max ( f1, f2) << endl;
string s1 = "Hello" ;
string s2 = "World" ;
cout << "Max(s1, s2): " << Max ( s1, s2) << endl;
return 0 ;
}
【 2. 类模板 】
泛型类 声明的一般形式如下所示: type 是占位符类型名称,可以在类被实例化的时候进行指定。我们可以使用一个逗号分隔的列表来定义多个泛型数据类型。
template < class ClassNameType > class ClassName
{
. . .
}
实例1:定义类 Stack,并实现了泛型方法来对元素进行入栈出栈操作。
# include <iostream>
# include <vector>
# include <cstdlib>
# include <string>
# include <stdexcept>
using namespace std;
template < class T > class Stack
{
private :
vector< T> elems;
public :
void push ( T const & ) ;
void pop ( ) ;
T top ( ) const ;
bool empty ( ) const
{
return elems. empty ( ) ;
}
} ;
template < class T > void Stack < T> :: push ( T const & elem)
{
elems. push_back ( elem) ;
}
template < class T > void Stack < T> :: pop ( )
{
if ( elems. empty ( ) )
{
throw out_of_range ( "Stack<>::pop(): empty stack" ) ;
}
elems. pop_back ( ) ;
}
template < class T > T Stack < T> :: top ( ) const
{
if ( elems. empty ( ) )
{
throw out_of_range ( "Stack<>::top(): empty stack" ) ;
}
return elems. back ( ) ;
}
int main ( )
{
try
{
Stack< int > intStack;
Stack< string> stringStack;
intStack. push ( 7 ) ;
cout << intStack. top ( ) << endl;
stringStack. push ( "hello" ) ;
cout << stringStack. top ( ) << std:: endl;
stringStack. pop ( ) ;
stringStack. pop ( ) ;
}
catch ( exception const & ex)
{
cerr << "Exception: " << ex. what ( ) << endl;
return - 1 ;
}
}
# include <iostream>
using namespace std;
template < class R , int n>
class Array
{
private :
R arr[ n] ;
public :
Array ( R * a)
{
for ( int i = 0 ; i < n; ++ i)
arr[ i] = a[ i] ;
}
Array ( void )
{
for ( int i = 0 ; i < n; ++ i)
cin >> arr[ i] ;
}
R sum ( void )
{
R total= 0 ;
for ( int i = 0 ; i < n; ++ i)
total += arr[ i] ;
return total;
}
R average ( void )
{
return ( * this ) . sum ( ) / n;
}
} ;
# pragma once
# include <cstring>
# include <iostream>
using namespace std;
template < typename T > T min ( T a, T b)
{
cout<< "普通模板 " ;
return a < b ? a : b;
}
template < > const char * min ( const char * a, const char * b)
{
cout << "特殊模板 " ;
return strcmp ( a, b) < 0 ? a: b;
}
# include <iostream>
# include "Arry.h"
# include "fun.h"
using namespace std;
int main ( void )
{
cout << min ( 3 , 5 ) << endl;
cout << min ( 3.2 , 5.4 ) << endl;
cout << min ( "C++" , "Arry" ) << endl;
int s[ 5 ] = { 0 , 1 , 2 , 3 , 4 } ;
Array< int , 5 > s1 ( s) ;
cout<< s1. sum ( ) << endl;
cout << s1. average ( ) << endl;
Array< int , 5 > s2;
cout << s2. sum ( ) << endl;
cout << s2. average ( ) << endl;
return 0 ;
}