The concept and significance of 56 μ function template

How many ways to exchange variables in C + +?

How to exchange variables

  • Define macro code block
  • Defined function

Programming experiment: exchange of variables

#include <iostream>
#include <string>

using namespace std;

#define SWAP(t, a, b)    \
{                        \
    t c = a;             \
    a = b;               \
    b = c;               \
}while{0}

void Swap(int& a, int& b)
{
    int c = a; 
    a = b;
    b = c;
}

void Swap(double& a, double& b)
{
    double c = a; 
    a = b;
    b = c;
}

void Swap(string& a, string& b)
{
    string c = a; 
    a = b;
    b = c;
}

int main()
{
    int a = 0;
    int b = 1;
    
    Swap(a, b);
    
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    
    double m = 2;
    double n = 3;
    
    Swap(m,n );
    
    cout << "m = " << m << endl;
    cout << "n = " << n << endl;
    
    string d = "D.T.";
    string t = "Software";
    
    Swap(d, t);
    
    cout << "d = " << d << endl;
    cout << "t = " << t << endl;

    return 0;
}
Output:
a = 1
b = 0
m = 3
n = 2
d = Software
t = D.T.

  • Define macro code block

    • Advantages: code reuse, suitable for all types
    • Disadvantages: compiler does not know the existence of macro, lack of type checking
  • Defined function

    • Advantage: true function call, compiler checks type
    • Disadvantage: the function is defined repeatedly according to the type and cannot be reused

New needs:
Is there a solution in C + + that combines the advantages of both methods?

Generic Programming

The concept of generic programming

  • Programming without considering specific data types

For Swap functions, you can consider the following generics

void Swap(T& a, T& b)
{
    T t = a;
    a = b;
    b = t;
}

T in Swap generic programming is not a specific data type, but an arbitrary data type.

Function template

Generic programming in C + +

  • Function template

    • A special function can be called with different types
    • It looks like a normal function, except that the type can be parameterized

template<typename T>
void Swap(T& a, T& b)
{
    T t = a;
    a = b;
    b = t;
}

Syntax rules for function templates

  • The template keyword is used to declare the start of generic programming
  • typename keyword is used to declare a generic type

Use of function templates

  • Automatic type derivation call
  • Specific type display call

void code()
{
    int a = 0;
    int b = 1;
    
    Swap(a, b);           // Automatic derivation
    
    float c = 2;
    float d = 3;
    
    Swap<float>(c, d);    // Display invocation
}

Programming experiment: a preliminary study on the use of function template

#include <iostream>
#include <string>

using namespace std;

template < typename T >
void Swap(T& a, T& b)
{
    T c = a;
    a = b;
    b = c;
}

template < typename T >
void Sort(T a[], int len)
{
    for(int i=0; i<len; i++)
    {
        for(int j=i; j<len; j++)
        {
            if( a[i] > a[j] )
            {
                Swap(a[i], a[j]);
            }
        }
    }
}

template < typename T >
void Println(T a[], int len)
{
    for(int i=0; i<len; i++)
    {
        cout << a[i] << ", ";
    }
    
    cout << endl;
}

int main()
{
    int a[5] = {5, 4, 3, 2, 1};
    
    Println<int>(a, 5);
    Sort<int>(a, 5);
    Println<int>(a, 5);
    
    string s[5] = {"Java", "C++", "Pascal", "Ruby", "Basic"};
    
    Println(s, 5);
    Sort(s, 5);
    Println(s, 5);
    
    return 0;
}
Output:
5, 4, 3, 2, 1, 
1, 2, 3, 4, 5, 
Java, C++, Pascal, Ruby, Basic, 
Basic, C++, Java, Pascal, Ruby, 

Summary

  • Function template is one of the application ways of generic programming in C + +
  • Function template can deduce parameter types according to actual parameters
  • Function template supports display of specified parameter types
  • Function template is an important way of code reuse in C + +

The above contents refer to the series courses of Ditai Software Institute, please protect the original!

Tags: C++ Programming Java Ruby

Posted on Sat, 30 Nov 2019 07:31:24 -0800 by mars_rahul