標籤:value 定義函數 一起 不同類 mes 泛型類 cto include ==
C++ 範本
模板是泛型程式設計的基礎,泛型程式設計即以一種獨立於任何特定類型的方式編寫代碼。
模板是建立泛型類或函數的藍圖或公式。庫容器,比如迭代器和演算法,都是泛型程式設計的例子,它們都使用了模板的概念。
每個容器都有一個單一的定義,比如 向量,我們可以定義許多不同類型的向量,比如 vector <int> 或 vector <string>。
您可以使用模板來定義函數和類,接下來讓我們一起來看看如何使用。
1 #include <iostream> 2 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 6 7 int main(int argc, char** argv) { 8 int max_value(int x,int max); 9 int i,j,row=0,colum=0,max;10 int a[3][4]={{5,12,23,56},{19,28,37,46},{-12,-34,6,8}};11 max=a[0][0];12 for(i=0;i<=2;i++)13 for(j=0;j<=3;j++)14 {15 max=max_value(a[i][j],max);16 if(max==a[i][j])17 {18 row=i;19 colum=j;20 }21 }22 cout <<"max="<<max<<"/row="<<row<<",colum="<<colum<<endl;23 return 0;24 }25 26 int max_value(int x,int max)27 {28 if(x>max) return x;29 else return max;30 }
C++ 範本