, , . , , , . , . template. , <>. class, .
, .
template <class Ttype> _( )
{/* */}
Ttype , , , , . , , . :
#include <iostream.h>// 50
#include <string.h>
template <class X> int find(X object, X *list, int size) {
int i;
for(i=0; i<size; i++)
if(object == list[i]) return i;
return -1;
}
int main(){
int a[ ]={1, 2, 3, 4};
char *c=" ";
double d[ ]={1.1, 2.2, 3.3};
cout << find(3, a, 4) << endl;
cout << find('a', c, strlen(c))<< endl;
cout << find(0.0, d, 3);
return 0;
}
find(), .
, :
template <class type> type abs (type x) { return x > 0? x: -x;}
.
template <class T> void swap (T* x, T* y){
T z = *x;
*x = *y;
*y = x; }
T , , z.
swap() :
long k = 4, d = 8; swap (&k, &d);
:
void swap (long* x, long* y){
long z = *x;
*x = *y;
*y = z; }
, k d .
:
double a = 2.44, b = 66.3; swap (&a, &b);
void swap (double* x, double* y){
double x = *x;
*x = *y;
*y = x;
}