1.模板
(1)简介
int main ( ) { return 0 ;
}
(2)函数模板
template < typename T >
void mySwap ( T & a, T & b) { T temp= a; a= b; b= temp;
}
template < typename T >
void func ( ) { cout<< "fun 调用" << endl;
}
void test01 ( ) { int a= 10 ; int b= 20 ; mySwap ( a, b) ; mySwap < int > ( a, b) ; cout<< "a=" << a<< endl; cout<< "b=" << b<< endl;
}
int main ( ) { test01 ( ) ; func < int > ( ) ; return 0 ;
}
(3)函数模板案例
template < class T >
void mySort ( T arr[ ] , int len) { for ( int i= 0 ; i< len; i++ ) { int max= i; for ( int j= i+ 1 ; j< len; j++ ) { if ( arr[ max] < arr[ j] ) { max= j; } } if ( max!= i) { T temp= arr[ max] ; arr[ max] = arr[ i] ; arr[ i] = temp; } }
}
template < typename T >
void sayArray ( T arr[ ] , int len) { cout << "数组元素为:" << sizeof ( arr) << endl; for ( int i = 0 ; i < len; i++ ) { cout << arr[ i] << endl; }
} void test01 ( ) { char charArr[ ] = "abcdef" ; int num= sizeof ( charArr) / sizeof ( char ) ; mySort ( charArr, num) ; sayArray ( charArr, num) ;
} int main ( ) { test01 ( ) ; return 0 ;
}
(4)普通模板和函数模板的调用规则
void myPrint ( int a, int b) { cout << "调用的普通函数" << endl;
} template < typename T >
void myPrint ( T a, T b) { cout << "调用的模板" << endl;
}
template < typename T >
void myPrint ( T a, T b, int ) { cout << "调用的模板" << endl;
} int main ( ) { myPrint ( 1 , 2 ) ; myPrint < > ( 2 , 2 ) ; myPrint ( '1' , 'a' ) ; return 0 ;
}
(5)模板的局限性
class Person {
public : string name; int age; Person ( string name, int age) { this -> name = name; this -> age = age; } bool operator == ( Person & p) { cout<< "operator== 函数被调用了" << endl; if ( this -> name == p. name && this -> age == p. age) { return true ; } else { return false ; } }
} ;
template < class T >
bool myCompare ( T & a, T & b) { if ( a == b) { return true ; } else { return false ; }
} void test01 ( ) { int a = 20 ; int b = 20 ; bool ret = myCompare ( a, b) ; if ( ret) { cout << "a==b" << endl; } else { cout << "a!=b" << endl; }
} void test02 ( ) { Person p1 ( "张三" , 10 ) ; Person p2 ( "李四" , 10 ) ; bool ret = myCompare ( p1, p2) ; cout << "p1==p2:" << ret << endl;
} int main ( ) { test01 ( ) ; test02 ( ) ; return 0 ;
}
(7)类模板
(1)类模板开胃菜
template < class nameType , class ageType >
class Person {
public : nameType name; ageType age; Person ( nameType name, ageType age) { this -> name = name; this -> age = age; } void showPerson ( ) { cout << "name=" << this -> name << ";age=" << age << endl; }
} ; void test01 ( ) { Person< string, int > p1 ( "孙大圣" , 28 ) ; p1. showPerson ( ) ; Person p2 ( "孙大圣2" , "282" ) ; p2. showPerson ( ) ;
} int main ( ) { test01 ( ) ; return 0 ;
}
(2)类模板与函数模板的区别
template < class nameType , class ageType = string>
class Person {
public : nameType name; ageType age; Person ( nameType name, ageType age) { this -> name = name; this -> age = age; } void showPerson ( ) { cout << "name=" << this -> name << ";age=" << age << endl; }
} ;
void test01 ( ) { Person p1 ( "孙悟空" , 1000 ) ;
}
void test02 ( ) { Person p2 ( "aaa" , 123 ) ; p2. showPerson ( ) ; Person p3 ( "bbb" , '234' ) ; p3. showPerson ( ) ;
} int main ( ) { test02 ( ) ; return 0 ;
}
(3)类模板中成员函数创建时机
class Person1 {
public : void showPerson1 ( ) { cout << "Person1" << endl; }
} ; class Person2 {
public : void showPerson2 ( ) { cout << "Person2 show" << endl; }
} ; template < class T >
class myClass {
public : T obj; void func1 ( ) { obj. showPerson1 ( ) ; } void func2 ( ) { obj. showPerson2 ( ) ; }
} ; void test01 ( ) { myClass< Person1> m; m. func1 ( ) ; myClass< Person2> m2; m2. func2 ( ) ;
} int main ( ) { test01 ( ) ; return 0 ;
}
(4)类模板对象做函数参数
* * 类模板对象做函数参数* * 学习目标:类模板实例化出的对象,向函数传参的方式* * 三种方式:* 1. 指定传入的类型:直接显示对象的数据类型* 2. 参数模板化: 将对象中的参数变为模板进行传递* 3. 整个类模板化:将这个对象类型 模板化进行传递* * / template < class T1 , class T2 >
class Person {
public : T1 name; T2 age; Person ( T1 name, T2 age) { this -> name = name; this -> age = age; } void showPerson ( ) { cout << "name=" << name << ";age=" << age << endl; }
} ;
void printPerson1 ( Person< string, int > & p) { p. showPerson ( ) ;
} void test01 ( ) { Person< string, int > p ( "孙悟空" , 100 ) ; printPerson1 ( p) ;
} template < class T1 , class T2 >
void printPerson2 ( Person< T1, T2> & p) { p. showPerson ( ) ; cout << "T1类型为:" << typeid ( T1) . name ( ) << endl; cout << "T2类型为:" << typeid ( T1) . name ( ) << endl;
}
void test02 ( ) { Person< string, int > p ( "猪八戒" , 90 ) ; printPerson2 ( p) ;
} template < class T >
void printPerson3 ( T p) { p. showPerson ( ) ;
}
void test03 ( ) { Person< string, int > p ( "唐山" , 20 ) ; printPerson3 ( p) ;
} int main ( ) { cout << "test01()" << endl; test01 ( ) ; cout << "test02()" << endl; test02 ( ) ; cout << "test03()" << endl; test03 ( ) ; return 0 ;
}
(5)类模板与继承
template < class T >
class Base { T m;
} ;
class Son : public Base < int > { } ; void test01 ( ) { Son son;
}
template < class T1 , class T2 >
class Son2 : public Base < T2 > {
public : T1 obj; Son2 ( ) { cout << "T1类型为:" << typeid ( T1) . name ( ) << endl; cout << "T2类型为:" << typeid ( T2) . name ( ) << endl; }
} ; void test02 ( ) { Son2< int , char > s2;
} int main ( ) { test01 ( ) ; test02 ( ) ; return 0 ;
}
(6)类模板成员函数类外实现
template < class T1 , class T2 >
class Person {
public : T1 name; T2 age; Person ( T1 name, T2 age) ; void showPerson ( ) ;
} ;
template < class T1 , class T2 >
Person < T1, T2> :: Person ( T1 name, T2 age) { this -> name= name; this -> age= age;
}
template < class T1 , class T2 >
void Person < T1, T2> :: showPerson ( ) { cout<< "姓名:" << name<< ";年龄=" << age<< endl;
} void test01 ( ) { Person< string, int > p ( "AAA" , 18 ) ; p. showPerson ( ) ; Person p2 ( "AAA" , 18 ) ; p2. showPerson ( ) ;
} int main ( ) { test01 ( ) ; return 0 ;
}
(7)类模板与友元
template < class T1 , class T2 >
class Person ;
template < class T1 , class T2 >
void printPerson2 ( Person< T1, T2> p) { cout << "类外实现:姓名:" << p. name << ";年龄:" << p. age << endl;
}
template < class T1 , class T2 >
class Person { friend void printPerson ( Person< T1, T2> p) { cout << "姓名:" << p. name << ";年龄:" << p. age << endl; } friend void printPerson2 ( ) ; public : T1 name; T2 age; Person ( T1 name, T2 age) { this -> name = name; this -> age = age; }
} ;
void test01 ( ) { Person p1 ( "Tom" , 18 ) ; printPerson ( p1) ;
}
void test02 ( ) { Person p2 ( "Dog" , 22 ) ; printPerson ( p2) ;
} int main ( ) { test01 ( ) ; test02 ( ) ; return 0 ;
}
(8)类模板案例
未学习
https: