模板应用案例
template
void swapl(T &a,T &b)
{
T temp=a;
a=b;
b=temp;
}
template
void mysort(T arr[],int len)
{
for(int i=0;i { int max=i; for(int j=i+1;j { if(arr[max]
{ max=j; } } if(max!=i) { swapl(arr[i],arr[max]); } } } template void printf(T arr[],int len) { for(int i=0;i { cout<
} cout< } void test() { char chararr[]="abdfre"; int num=sizeof(chararr)/sizeof(char); mysort(chararr,num); printf(chararr,num); } int main() { test(); system("pause"); return 0; } 函数模板 template T myadd(T a,T b) { return a+b; } void test() { int a=1; int b=2; char c='c'; cout< } int main() { test(); system("pause"); return 0; } 并不是所有的都可以直接用函数 class Person { public: Person(string name,int age) { this->m_age=age; this->m_name=name; } string m_name; int m_age; }; template bool mycompare(T &a,T &b) { if(a==b) { return true; } else { return false; } } template<> bool mycompare(Person &p1,Person &p2) { if(p1.m_name==p2.m_name&&p1.m_age==p2.m_age) { return true; } else { return false; } } void test01() { int a=10;int b=20; bool ret=mycompare(a,b); if(ret) { cout<<"niniu"; } else { cout<<"lala"; } } void test02() { Person p1("to",10); Person p2("to",10); bool ret=mycompare(p1,p2); if(ret) { cout<<"6"; } else { cout<<"88"; } } int main() { test02(); system("pause"); return 0; } 类模板 template class Person { public: Person(NameType name,AgeType age) { this->m_Name=name; this->m_Age=age; } void showPerson() { cout< } NameType m_Name; AgeType m_Age; }; test01() { Person p.showPerson(); } int main() { test01(); system("pause"); return 0; } 类模板对象做函数参数 class Person1 { public: void showPerson1() { cout<<"Person1 show"< } }; class Person2 { public: void showPerson2() { cout<<"Person2 show"< } }; template class MyClass { public: T obj; void func1() { obj.showPerson1(); } void func2() { obj.showPerson2(); } }; void test01() { MyClass m.func1(); //m.func2(); } int main() { test01(); system("pause"); return 0; } 类模板对象做函数参数 template class Person { public: Person(T1 name,T2 age) { this->m_Name=name; this->m_Age=age; } void showPerson() { cout<<"姓名"< } T1 m_Name; T2 m_Age; }; void printPerson1(Person { p.showPerson(); } void test01 类模板与继承 template class Base { T m; }; class Son:public Base { public: }; void test01() { Son s1; } template class Son2:public Base { public: Son2() { cout< } T1 obj; }; void test02() { Son2 } int main() { test02(); system("pause"); return 0; } 类模板成员函数类外实现 template class Person { public: Person(T1 name,T2 age); void showPerson(); T1 m_Name; T2 m_Age; }; template Person { this->m_Name=name; this->m_Age=age; } template void Person { cout< } void test01() { Person p.showPerson(); } int main() { test01(); system("pause"); return 0; } Hpp 类模板友元 #include #include #include #include #define MAX 1000 #include #include #define FILE "s111.txt" using namespace std; template class Person { friend void printPerson(Person { cout< } public: Person(T1 name,T2 age) { this->m_Name=name; this->m_Age=age; } private: T1 m_Name; T2 m_Age; }; void test01() { Person printPerson(p); } int main() { test01(); system("pause"); return 0; } 类模板案例,数组类封装 #include #include #include #include #include #include #define FILE "s111.txt" using namespace std; template class MyArray { public: MyArray(int capacity) { cout<<"有参构造"< this->m_Capacity=capacity; this->m_Size=0; this->pAddress=new T[this->m_Capacity]; } MyArray(const MyArray&arr) { cout<<"拷贝构造"< this->m_Capacity=arr.m_Capacity; this->m_Size=arr.m_Size; this->pAddress=new T[arr.m_Capacity]; for(int i=0;i { this->pAddress[i]=arr.pAddress[i]; } } MyArray &operator=(const MyArray&arr) { cout<<"operator构造"< if(this->pAddress!=NULL) { delete[]this->pAddress; this->pAddress=NULL; this->m_Capacity=0; this->m_Size=0; } this->m_Capacity=arr.m_Capacity; this->m_Size=arr.m_Size; this->pAddress=new T[arr.m_Capacity]; for(int i=0;i { this->pAddress[i]=arr.pAddress[i]; } return *this; } void Push_Back(const T&val) { if(this->m_Capacity==this->m_Size) { return; } this->pAddress[this->m_Size]=val;//在数组末尾插入数据 this->m_Size++; } void pop_Back() { if(this->m_Size==0) { return; } this->m_Size--; } T &operator[](int index) { return this->pAddress[index]; } int getCapacity() { return this->m_Capacity; } int getSize() { return this->m_Size; } ~MyArray() { if(this->pAddress!=NULL) { cout<<"析构"< delete[] this->pAddress; this->pAddress=NULL; } } private: T*pAddress; int m_Capacity; int m_Size; }; void printInArray(MyArray { for(int i=0;i { cout< } } void test01() { MyArray for(int i=0;i<5;i++) { arr1.Push_Back(i); } cout<<"输出"< printInArray(arr1); cout<<"容量"< cout<<"arr1的大小"< MyArray cout<<"输出2"< printInArray(arr2); arr2.pop_Back(); cout<<"arr尾删除后"< cout<<"容量"< cout<<"arr的大小"< } class Person { public: Person(){}; Person(string name,int age) { this->m_Name=name; this->m_Age=age; } string m_Name; int m_Age; }; void printPersonArray(MyArray { for(int i=0;i { cout<<"姓名"< } } void test02() { MyArray Person p1("孙悟空",999); Person p2("韩信",999); Person p3("啦啦啦信",999); Person p4("韩222信",999); Person p5("啦啦20132啦信",999); arr.Push_Back(p1); arr.Push_Back(p2); arr.Push_Back(p3); arr.Push_Back(p4); arr.Push_Back(p5); printPersonArray(arr); cout<<"array的容量"< cout<<"arr的容量大小"< } int main() { test02(); system("pause"); return 0; }类模板用



