C/C++语言是一种通用的编程语言,具有高效、灵活和可移植等特点。C语言主要用于系统编程,如操作系统、编译器、数据库等;C语言是C语言的扩展,增加了面向对象编程的特性,适用于大型软件系统、图形用户界面、嵌入式系统等。C/C++语言具有很高的效率和控制能力,但也需要开发人员自行管理内存等底层资源,对于初学者来说可能会有一定的难度。
函数模板的基本使用: 函数模板就是要实现类型参数化,实现泛型编程,就是可以动态的调整数据类型.
#include <iostream> #include <typeinfo> using namespace std ; template<class T> void MySwap (T &x, T &y) { T tmp = x; x = y; y = tmp; } template<class T> T MyAdd (T &x, T &y) { return x + y; } template<typename T> void MyPrint () { T number; cout << "type = " << typeid(T).name() << endl ; } int main (int argc, char *argv[]) { int x = 10 , y = 20 ; MySwap(x, y); cout << "x= " << x << endl ; int ret = MyAdd(x, y); cout << "x+y = " << ret << endl ; MySwap<int >(x, y); cout << "x= " << x << endl ; MyPrint<int >(); MyPrint<double >(); system("pause" ); return 0 ; }
我们在上面的案例基础上进行一定的加强,通过模板实现一个选择排序,我们可以传入任意的数据类型,都可被解析.
#include <iostream> #include <typeinfo> using namespace std ; template<class T> void MySwap (T &x, T &y) { T tmp = x; x = y; y = tmp; } template<class T> void SelectSort (T Array[], int len) { for (int x = 0 ; x < len; x++) { int max = x; for (int y = x + 1 ; y < len; y++) { if (Array[max] > Array[y]) max = y; } if (max != x) MySwap(Array[max], Array[x]); } } template<class T> void MyPrint (T Array[], int len) { for (int x = 0 ; x < len; x++) cout << Array[x] << " " ; } int main (int argc,char *argv[]) { int Int_Array[10 ] = { 4 , 7 , 8 , 2 , 1 , 8 , 0 , 3 , 2 , 7 }; SelectSort<int >(Int_Array, 10 ); MyPrint<int >(Int_Array, 10 ); char Char_Array[] = "hello lyshark" ; int len = sizeof (Char_Array) / sizeof (char ); SelectSort<char >(Char_Array, len); MyPrint<char >(Char_Array, len); system("pause" ); return 0 ; }
实现模板具体化: 通过自定义模板函数,解决模板的局限性问题.
#include <iostream> using namespace std ; class Student { public: char *m_name; int m_age; public: Student(char *name, int age) { this->m_name = name; this->m_age = age; } }; template<class Student> bool MyCompare (Student &x, Student &y) { if (x.m_age == y.m_age) return true ; return false ; } int main (int argc, char *argv[]) { Student stu1 ("lyshark" , 22 ) ; Student stu2 ("admin" , 33 ) ; bool ret = MyCompare(stu1, stu1); cout << ret << endl ; bool ret1 = MyCompare(stu1, stu2); cout << ret1 << endl ; system("pause" ); return 0 ; }
定义并使用类模板: 类模板不支持类型的自动推导,所以必须在调用时Student<string, int>
显式指定好类型.
#include <iostream> #include <string> using namespace std ; template<class NameType = string ,class AgeType = int > class Student { public: string m_name; int m_age; public: Student(NameType name,AgeType age) { this->m_name = name; this->m_age = age; } void show () { cout << "name = " << m_name << endl ; } }; template<class Student> bool MyCompare (Student &x, Student &y) { if (x.m_age == y.m_age) return true ; return false ; } int main (int argc, char *argv[]) { Student<string , int > stu1 ("lyshark" , 25 ) ; stu1.show(); system("pause" ); return 0 ; }
类模板做函数参数传递: 此处我们将类模板Student<string, int>
当做函数参数传递给MyPrint
函数.
#include <iostream> #include <string> using namespace std ; template<class NameType ,class AgeType > class Student { public: string m_name; int m_age; public: Student(NameType name,AgeType age) { this->m_name = name; this->m_age = age; } void show () { cout << "name = " << m_name << endl ; } }; void MyPrintA (Student<string , int > &ptr) { ptr.show(); } template<class T1,class T2> void MyPrintB (Student<T1,T2> &ptr) { ptr.show(); } template<class T> void MyPrintC (T &ptr) { ptr.show(); } int main (int argc, char *argv[]) { Student<string , int > stu1 ("lyshark" , 25 ) ; MyPrintA(stu1); Student<string , int > stu2 ("admin" , 10 ) ; MyPrintB(stu2); Student<string , int > stu3 ("root" , 10 ) ; MyPrintC(stu3); system("pause" ); return 0 ; }
类模板类内定义类外实现: 类模板同样支持类内定义模板类型,在类外部对其进行具体的实现.
#include <iostream> #include <string> using namespace std ; template<class NameType ,class AgeType > class Student { public: string m_name; int m_age; public: Student(NameType name, AgeType age); void show () ; }; template <class NameType ,class AgeType > Student<NameType, AgeType>::Student(NameType name, AgeType age) { this->m_name = name; this->m_age = age; } template <class NameType ,class AgeType > void Student<NameType, AgeType>::show(){ cout << "Name = " << this->m_name << endl ; } int main (int argc, char *argv[]) { Student<string , int > stu ("lyshark" , 20 ) ; stu.show(); system("pause" ); return 0 ; }
类模板友元函数类内实现: 友元函数就是可以让类外直接访问的函数,调用类内友元函数就像调用全局函数一样.
#include <iostream> #include <string> using namespace std ; template<class NameType ,class AgeType > class Student { friend void show (Student<NameType, AgeType> &ptr) { cout << "name = " << ptr.m_name << endl ; } private: string m_name; int m_age; public: Student(NameType name, AgeType age) { this->m_name = name; this->m_age = age; } }; int main (int argc, char *argv[]) { Student<string , int > stu ("lyshark" , 20 ) ; show(stu); system("pause" ); return 0 ; }
类模板友元函数类外实现: 类外实现同理,就是现在类内声明类型作为占位符,然后在类外进行实现.
#include <iostream> #include <string> using namespace std ; template<class T1 , class T2 > class Student ; template<class T1, class T2> void show (Student<T1, T2> & p) ; template<class T1 ,class T2 > class Student { friend void show<>(Student<T1, T2> &ptr); private: string m_name; int m_age; public: Student(T1 name, T2 age) { this->m_name = name; this->m_age = age; } }; template<class T1,class T2> void show (Student<T1,T2> &ptr) { cout << "name = " << ptr.m_name << endl ; } int main (int argc, char *argv[]) { Student<string , int > stu ("lyshark" , 20 ) ; show(stu); system("pause" ); return 0 ; }