Template Beginner Guide 1

zhaozj2021-02-17  55

Template Beginner Guide 1

Original: http://www.codeproject.com/cpp/templates_part1.asp

When developing large applications, for different functions and classes, you can save a lot of time by using a shared code template. The template is independent of the data in a common function or a class. In this guide, I will process template functions and template classes. Suppose you have implemented a class processing stack, and this stack class can handle the value of the Double type. What should I do if I need a constitutive stack class? Without template technology, you have to copy this stack class code. Such efficiency is not high. With templates, you can define a template class or a function, use all functions and types, you can declare a new variable in the template definition. See how you work below:

Function template

Suppose we need a function template to find minimum values ​​in different types:

Template

ELMTYPE CALCMIN (ElemType Elemfield [], Intnesssize)

{

INT IMIN = 0;

For (int i = 1; i

{

IF (Elemfield [i]

iMIN = i;

}

Return Elemfield [iMin];

}

This is the function template. He looks forward to a data type and will return one of them. Use this template to see the following example:

void letstestthefunctionTemplate ()

{

Intness [] = {1, 2, 3, 4, 5, 6};

Double dfield [] = {2.5, 2.31, 10.23, 15.2};

Int isize1 = sizeof (ifield) / sizeof (int);

INT i = Calcmin (ifield, isize1);

Int isize2 = sizeof (dfield) / sizeof (double);

Double D = Calcmin (Dfield, ISize2);

}

Template MIN is used by two different data types. One is int [], and the Double [], but the function is functional. Find minimal and return minimum.

Function templates can also use Inline, Extern Static declaration. Note To put these Template keywords and parameters. as follows:

Template

Inline ElemType Swap (ElemType & A, ElemType & B);

Class template

Define class templates Similar definition function templates. Look at the example below, the general STACK class handles different types. The class is defined as follows:

Template

Class Stack

{

PUBLIC:

Stack ();

~ Stack ();

Void Push (Const ElemType & Anelement);

Void Pop (ElemType & Anelement);

Bool Waserror () Const;

Bool isempty () const;

Private:

ELMTYPE ELEMS [ISIZE];

INT ITOP;

Bool Berroroccd;

}

In addition to some symbols, this type of implementation is not much different from the usual implementation. When defining a class template, you can use a normal class. But you must specify parameters in . In the template, the class name can be used without parameters. Look at the implementation of the following: // include your prototype here or use a #define

Template

Stack :: stack ()

: ITOP (0), Berroroccd (False)

{

}

Template

Stack :: ~ stack ()

{

}

Template

Void Stack :: Push (const elemtype & anelement)

{

Berroroccd = (iTOP == isize);

IF (! Berroroccd)

ELEMS [ITOP ] = AneElement;

}

Template

Void Stack :: POP (ElemType & Anelement)

{

Berroroccd = (iTOP == 0);

IF (! Berroroccd)

Anelement = Elems [- ITOP];

}

Template

Bool Stack :: WasError () Const

{

Return Berroroccd;

}

Template

Bool Stack :: isempty () Const

{

Return (ITOP == 0);

}

Use the class template as follows:

Stack itheintstack;

Stack DTHEDOUBLESTACK;

Sentence: The advanced function template is described below. For example, the template contains other templates, and the like.

转载请注明原文地址:https://www.9cbs.com/read-30042.html

New Post(0)