C + + templates

Source: Internet
Author: User

C + + templates
Templates are the basis of generic programming, which is to write code in a way that is independent of any particular type.

A template is a blueprint or formula for creating a generic class or function. Storage converters, such as iterators and algorithms, are examples of generic programming that use the concept of templates.

Each container has a single definition, such as vectors, we can define many different types of vectors, such as vector <int> or vector <string>.

You can use templates to define functions and classes, and then let's take a look at how to use them.

1#include <iostream>2 3 /*Run this program using the console Pauser or add your own getch, System ("pause") or input loop*/4 using namespacestd;5 6 7 intMainintargcChar**argv) {8     intMax_value (intXintmax);9     inti,j,row=0, colum=0, Max;Ten     inta[3][4]={{5, A, at, About},{ +, -,Panax Notoginseng, $},{- A,- the,6,8}}; Onemax=a[0][0]; A      for(i=0; i<=2; i++) -      for(j=0; j<=3; j + +) -     { themax=Max_value (A[i][j],max); -         if(max==A[i][j]) -         { -row=i; +colum=J; -         } +     } Acout <<"max="<<max<<"/row="<<row<<", colum="<<colum<<Endl; at     return 0; - } -  - intMax_value (intXintmax) - { -     if(X>max)returnx; in     Else returnMax; -}

C + + templates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.