The concept and significance of function template in the 56th lesson

Source: Internet
Author: User
Tags define function

1. divergence Problem: A method of several exchange variables in C + +

(1) macro code block VS function

Exchange of "programming experiment" variables

#include <iostream>#include<string>using namespacestd;//macro Definition code block#defineSWAP (T, a, B) Do{T C=A; A=b; b=C; } while(0);//define function ModevoidSwap (int& A,int&b) {    intc =A; A=b; b=C;}voidSwap (Double& A,Double&b) {    Doublec =A; A=b; b=C;}voidSwap (string& A,string&b) {    stringc =A; A=b; b=C;}intMain () {intA =0; intb =1;   Swap (A, b); //Swap (int&, int&)cout<<"A ="<< a <<Endl; cout<<"B ="<< b <<Endl; SWAP (int, A, b);//Macro Codecout<<"A ="<< a <<Endl; cout<<"B ="<< b <<Endl; Doublem =2; Doublen =3;  Swap (M, n); //Swap (double&, double&)cout <<"m ="<< m <<Endl; cout<<"n ="<< N <<Endl; SWAP (Double, M, n);//Macro Codecout <<"m ="<< m <<Endl; cout<<"n ="<< N <<Endl; strings ="Santa"; stringc ="Claus";        Swap (S, c); cout<<"s ="<< s <<Endl; cout<<"C ="<< C <<Endl; SWAP (string, S, c); cout<<"s ="<< s <<Endl; cout<<"C ="<< C <<Endl; return 0;}

(2) Comparison of advantages and disadvantages

Hong Yihong code block

Defining functions

Advantages

code reuse for all types

A real function call , the compiler checks the type

Disadvantages

compiler does not know the existence of macros, missing type checking

Duplicate a function by type, cannot code reuse

2. New Solution-Generic programming

(1) Regardless of the specific data types of programming, the combination of the advantages of the above two scenarios.

(2) function template: Generics in C + +

① a special function can be called with different types

② looks similar to normal functions, except that types can be parameterized , such as

Template<typename t>

void Swap (t& A, t& B)

(3) Syntax rules for function templates

The ①template keyword is used to declare the beginning of generic programming

TypeName keyword for declaring a generic type

3. Use of function templates

(1) automatic type deduction call

int a = 0, b = 1;

Swap (A, b); Automatic derivation

(2) Explicit invocation of a specific type

float c = 2, d = 3;

Swap<float> (c, D); Explicitly called

On the use of "programming experiment" function template

#include <iostream>#include<string>using namespacestd;template<typename t>voidSwap (t& A, t&b) {T C=A; A=b; b=C;} Template<typename t>voidSelectsort (T a[],intLen) {     for(inti =0; i < Len; i++)    {         for(intj = i; J < Len; J + +)        {            if(A[i] < a[j])//Descending{Swap (a[i], a[j]); }}}}template<typename t>voidPrintln (T a[],intLen) {     for(inti =0; i < Len; i++) {cout<< A[i] <<", "; } cout<<Endl;}intMain () {inta[5] = {5,3,2,4,1}; Println (A,5); Selectsort (A,5); Println (A,5); strings[5] = {"Java","C + +","Pascal","Ruby","Basic"}; Println (s),5); Selectsort (s),5); Println (s),5); return 0;}

4. Summary

(1) function template is one of the ways that generic programming is applied in C + +

(2) function template can deduce parameter types based on actual parameters

(3) Function templates support explicitly specifying parameter types

(4) Function template is an important method of code reuse in C + +

The concept and significance of function template in the 56th lesson

Related Article

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.