Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Templates

C++ Templates

Templates are the foundation of generic programming, which is a way of writing code independent of any specific type.

A template is a blueprint or formula for creating generic classes or functions. Library containers, such as iterators and algorithms, are examples of generic programming that use the concept of templates.

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

You can use templates to define functions and classes; let's see how to use them next.

Function Templates

The general form of a function template definition is shown below:

template

<

typename

type

ret

type

func

name

(

parameter

list

)

{

// body of the function

}

Here, type is a placeholder name for the data type used by the function. This name can be used within the function definition.

Below is an example of a function template that returns the maximum of two numbers:

Example

#include

<

iostream

#include

<

string

using

namespace

std

;

template

<

typename

T

inline

T

const

&

Max

(

T

const

&

a

,

T

const

&

b

)

{

return

a

<

b

?

b

:

a

;

}

int

main

(

)

{

int

i

=

39

;

int

j

=

20

;

cout

<<

"

Max(i, j):

"

<<

Max

(

i

,

j

)

<<

endl

;

double

f1

=

13

.5

;

double

f2

=

20

.7

;

cout

<<

"

Max(f1, f2):

"

<<

Max

(

f1

,

f2

)

<<

endl

;

string

s1

=

"

Hello

"

;

string

s2

=

"

World

"

;

cout

<<

"

Max(s1, s2):

"

<<

Max

(

s1

,

s2

)

<<

endl

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:



Max(i, j): 39

Max(f1, f2): 20.7

Max(s1, s2): World

Class template

Just as we define function templates, we can also define class templates. The general form of a generic class declaration is shown below:



template  class class-name {

.

.

.

}

Here,type is a placeholder type name that can be specified when the class is instantiated. You can use a comma-separated list to define multiple generic data types.

The example below defines the class Stack<>, and implements generic methods to push and pop elements onto and off the stack:

Example

#include

<

iostream

#include

<

vector

#include

<

cstdlib

#include

<

string

#include

<

stdexcept

using

namespace

std

;

template

<

class

T

class

Stack

{

private

:

vector

<

T

elems

;

// element

public

:

void

push

(

T

const

&

)

;

// Push

void

pop

(

)

;

// Pop

T

top

(

)

const

;

// return the top element

bool

empty

(

)

const

{

// return true if empty.

return

elems

.

empty

(

)

;

}

}

;

template

<

class

T

void

Stack

<

T

::

push

(

T

const

&

elem

)

{

// append a copy of the passed element

elems

.

push_back

(

elem

)

;

}

template

<

class

T

void

Stack

<

T

::

pop

(

)

{

if

(

elems

.

empty

(

)

)

{

throw

out_of_range

(

"

Stack<>::pop(): empty stack

"

)

;

}

// Delete the last element

elems

.

pop_back

(

)

;

}

template

<

class

T

T

Stack

<

T

::

top

(

)

const

{

if

(

elems

.

empty

(

)

)

{

throw

out_of_range

(

"

Stack<>::top(): empty stack

"

)

;

}

// return a copy of the last element

return

elems

.

back

(

)

;

}

int

main

(

)

{

try

{

Stack

<

int

intStack

;

// stack of int type

Stack

<

string

stringStack

;

// stack of string type

// operate on the int stack

intStack

.

push

(

7

)

;

cout

<<

intStack

.

top

(

)

<<

endl

;

// operate on the string stack

stringStack

.

push

(

"

hello

"

)

;

cout

<<

stringStack

.

top

(

)

<<

std

::

endl

;

stringStack

.

pop

(

)

;

stringStack

.

pop

(

)

;

}

catch

(

exception

const

&

ex

)

{

cerr

<<

"

Exception:

"

<<

ex

.

what

(

)

<<

endl

;

return

1

;

}

}

When the code above is compiled and executed, it produces the following result:



7

hello

Exception: Stack<>::pop(): empty stack