C++ 範本

來源:互聯網
上載者:User

標籤: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++ 範本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.