Function template function arguments do

First, the function template affirms that generate and precautions

1.1 function template declared

Function template can be used to create a generic function to support a variety of different parameter, to avoid duplication of function body overloaded function design. Its biggest feature is a function of the type of data used as parameters.

Function template declaration form is:

  template <typename parameter data type identifier>

  <Return type> <function name> (parameter table)

    {

        Function body

    }

  Wherein, the keyword is defined template template function; angle brackets behind the template can not be omitted; typename (or class) is the keyword to declare the data type of parameter identifier, an identifier for indicating that it is behind the data type identifier. Thus, in this function later defined, wishing to determine the data type of the variable parameter according to the actual data types, data types can be described with the parameter identifier, so that this variable can be adapted to different types of data.

Function template declaration just described, that is a function template, not a function that can be executed directly, and only after an argument with a data type instead of the type parameter identifier in order to have a real function according to the actual situation. You can also use the keyword typename keyword class, then the data type identifier can use all the parameters of the C ++ data types.

Generating 1.2 template functions

  Parameter data type identifier is actually a function template type parameter, when using a template function, to instantiate the parameter data type determination. Examples of the type parameter is a parameter called template argument by argument instantiated template function is called template function. Generating the template function is a function template type parameter instantiation process.

1.3 function template should pay attention to the problem

  1) Function templates allow multiple types of parameters, but there must be a keyword typename before each class or template-shaped portion defined parameters, namely:

    template <class parameter data type identifier 1, ..., class data type identifier parameter n>

    <Return type> <function name> (parameter table)

    {

         Function body

    }

  2 does not allow other statements) with the template function template definition statement statements <return type> between. Such as the following statement is wrong:

. 1 Template < class T>
 2  int the I;
 . 3  T min (T X, T Y)
 . 4  
. 5  {
 . 6  
. 7     function body
 . 8  
. 9 }

  3) template function similar overloaded function, but both are very different: when overload function, each function can perform different actions in vivo, but the same function after a template instantiated template function must perform the same operation .

Second, the function template calls

Template function calls are usually two ways to call:

  1) myswap <float> (a, b); // call display type

  2) myswap (a, b); // automatic data type inference

Third, the function template parameter instances do

  3.1 pairs to sort an array, not using a template

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  
. 4  // sorted array of integers, an array of strings 
. 5  int mysort ( int * ARR, int len)
 . 6  {
 . 7      int tmp = 0 ;
 . 8      IF (ARR == NULL)
 . 9      {
 10          return - . 1 ;
 . 11      }
 12 is      for ( int I = 0 ; I <len; I ++ )
 13 is      {
 14          
15          for (int j = i+1; j < len; j++)
16         {
17             if (arr[i]<arr[j])
18             {
19                 tmp = arr[i];
20                 arr[i] = arr[j];
21                 arr[j] = tmp;
22             }
23         }
24     }
25 
26     return 0;
27 }
28 int myPrint(int *arr,int len)
29 {
30     
31     for (int i = 0; i < len; i++)
32     {
33         cout << arr[i] << "    " ;
34     }
35     cout << endl;
36     return 0;
37 }
38 void main()
39 {
40     int arr[] = {11,33,4,55,66,78,98};
41     intsize = the sizeof (ARR) / the sizeof (* ARR);
 42 is      the printf ( " Before sorting \ n- " );
 43 is      the myPrint (ARR, size);
 44 is      the printf ( " after sorting \ n- " );
 45      mysort (ARR, size) ;
 46 is      the myPrint (ARR, size);
 47      System ( " PAUSE " );
 48 }

3.2 Examples of function template parameters do

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  
. 4  // sorted array of integers, an array of strings 
. 5 Template <typename T, typename T2>
 . 6  int mysort (T * ARR, T2 len)
 . 7  {
 . 8      T tmp = 0 ;
 . 9      IF (ARR == NULL)
 10      {
 . 11          return - . 1 ;
 12 is      }
 13 is      for (T I = 0 ; I <len; I ++ )
 14      {
 15          
16          for (T j = i+1; j < len; j++)
17         {
18             if (arr[i]<arr[j])
19             {
20                 tmp = arr[i];
21                 arr[i] = arr[j];
22                 arr[j] = tmp;
23             }
24         }
25     }
26 
27     return 0;
28 }
29 template<typename T, typename T2>
30 int myPrint(T *arr,T len)
31 {
32     
33     for (T i = 0; i < len; i++)
34     {
35         cout << arr[i] << "    " ;
36     }
37     cout << endl;
38     return 0;
39 }
40 void main()
41 {
42     int arr[] = {11,33,4,55,66,78,98};
43     int size = sizeof(ARR) / the sizeof (* ARR);
 44 is      the printf ( " Before sorting \ n- " );
 45      the myPrint < int , int > (ARR, size); // call to the display
 46 is      the printf ( " after sorting \ n- " );
 47      mySort (arr, size); // automatically invoke
 48      the myPrint < int , int > (ARR, size);
 49      System ( " PAUSE " );
 50 }

 

Guess you like

Origin www.cnblogs.com/506941763lcj/p/11279062.html