標籤:

函數模板和類模板實例

函數模板定義:

template<classT>

//模板前綴,通知編譯器接下來的函數定應負或者函數聲明是一個模板,class此處不是類,而是類型(type).T是類型參數,可被任意類型替代(如int,char,double.....)

因此,函數模板實際是針對不同類型的函數的一個大集合.

Template<類型1 變數1 , 類型2 變數2…..> 返回類型函數名(形參表)

{

函數定義體;

}

編譯器根據該函數實參數據類型,生成相應的重載函數,該重載函數稱為模板函數,是一個實實在在的函數。

函數模板實例

//**********************

//**** 15_3.cpp****

//**********************

#include<iostream>

#include <string>

using namespace std;

template <typenameT>

Tmax(T a, T b, T c)

{

if (b > a)

{

a = b;

}

if (c > a)

{

a = c;

}

return a;

}

voidmain()

{

int i1, i2, i3;

cin >> i1>> i2>> i3;

int i = max(i1, i2, i3);

cout << "i_max = "<< i <<endl;

string g1, g2, g3;

cin >> g1>> g2>> g3;

string g = max(g1, g2, g3);

cout << "g_max = "<< g <<endl;

}

15.4 字元串轉換系統

//**********************

//**** 15_4.cpp****

//**********************

#include<iostream>

#include <string>

#include <sstream>

using namespace std;

template<typenameT>

TfromString(const string& s)

{

istringstream is(s);

T t;

is >> t;

return t;

}

template<typenameT>

string toString(const T& t)

{

ostringstream os;

os << t;

return os.str();

}

voidmain()

{

int i = 1234;

cout << "i = "<< toString(i)<< endl;

double x = 34.32;

cout << "x = "<< toString(x)<< endl;

i =fromString<int>(string("5678"));

cout << "i = "<< i <<endl;

x =fromString<double>(string("3.1415"));

cout << "x = "<< x <<endl;

}

15.5 矩陣運算

矩陣運算矩陣轉置與矩陣相乘

//**********************

//**** 15_5.cpp****

//**********************

#include<iostream>

#include <iomanip>

using namespace std;

voidinverse(int [3][6], int [6][3]);

voidmulti(int[6][3], int [3][4], int [6][4]);

voidoutput(int [6][4]);

intmain()

{

int middle[6][3], result[6][4];

int matrix1[3][6] = {8, 10, 12, 23, 1, 3, 5, 7, 9, 2, 4, 6, 34, 45,56, 2, 4, 6};

int matrix2[3][4] = {3, 2, 1, 0, -1, -2, 9, 8, 7, 6, 5,4};

inverse(matrix1, middle);

multi(middle, matrix2, result);

output(result);

return 0;

}

voidinverse(int matrix1[3][6], int middle[6][3])

{

int i, j;

for (i = 0; i < 3; i++)

{

for (j = 0; j < 6; j++)

{

middle[j][i] = matrix1[i][j];

}

}

}

voidmulti(int middle[6][3], int matrix2[3][4], intresult[6][4])

{

int i, j, k;

for (i = 0; i < 6; i++)

{

for (j = 0; j < 4; j++)

{

result[i][j] = 0;

for (k = 0; k < 3; k++)

{

result[i][j] += middle[i][k]*matrix2[k][j];

}

}

}

}

voidoutput(int result[6][4])

{

int i, j;

cout << "result"<< "
";

for (i = 0; i < 6; i++)

{

for (j = 0; j < 4; j++)

{

cout << setw(6)<< result[i][j];

}

cout << endl;

}

}

運算結果:

result

257210223176

338276298236

419342373296

81545124

27185748

45308772

矩陣運算:矩陣轉置與矩陣相乘函數模板。下標作為參數傳遞

//**********************

//**** 15_5.cpp****

//**********************

#include<iostream>

#include <iomanip>

using namespace std;

template<typename T1, typenameT2>

voidinverse(T1* mat1, T2* mat2, int a, int b);

template<typename T1, typenameT2>

voidmulti(T1* mat1, T2* mat2, T2* result, int a, int b, intc);

template <typenameT>

voidoutput(T* mat, char* s, int a, int b);

intmain()

{

int middle[6][3], result[6][4];

int matrix1[3][6] = {8, 10, 12, 23, 1, 3, 5, 7, 9, 2, 4, 6, 34, 45,56, 2, 4, 6};

int matrix2[3][4] = {3, 2, 1, 0, -1, -2, 9, 8, 7, 6, 5,4};

char* s1 = "result";

char* s2 = "middle";

inverse(matrix1, middle, 6, 3);

//顯式: inverse<int[6],int[3]>(matrix1, middle, 6,3)

multi(middle, matrix2, result, 6, 3, 4);

//顯式: multi<int[3],int[4]>(middle, matrix2, result, 6, 3,4);

output(matrix1, "matrix1", 3, 6);

output(middle, s2, 6, 3);

output(matrix2, "matrix2", 3, 4);

output(result, s1, 6, 4);

return 0;

}

template<typename T1, typenameT2>

voidinverse(T1* mat1, T2* mat2, int a, int b)

{

for (int i = 0; i < b; i++)

{

for (int j = 0; j < a; j++)

{

mat2[j][i] = mat1[i][j];

}

}

}

template<typename T1, typenameT2>

voidmulti(T1* mat1, T2* mat2, T2* result, int a, int b, intc)

{

for (int i = 0; i < a; i++)

{

for (int j = 0; j < c; j++)

{

result[i][j] = 0;

for (int k = 0; k < b; k++)

{

result[i][j] += mat1[i][k] * mat2[k][j];

}

}

}

}

template <typenameT>

voidoutput(T* mat, char* s, int a, int b)

{

cout << s<< endl;

for (int i = 0; i < a; i++)

{

for (int j = 0; j < b; j++)

{

cout << setw(6)<< mat[i][j];

}

cout << endl;

}

}

運算結果:

matrix1

810122313

579246

344556246

middle

8534

10745

12956

2322

144

366

matrix2

3210

-1-298

7654

result

257210223176

338276298236

419342373296

81545124

27185748

45308772

類模板的一般定義形式為:

template <類型形參表> class 類名

{

類聲明體;

}

建立類模板之後,可以用類型實參定義模板類,並創建實例。模板類是一個實實在在的類,對象是該模板類的勢力。其格式如下:

類名 <類型實參表> 對象;

類模板實例

1:定義數組類模板

//**********************

//**** 15_6.cpp****

//**********************

#include<iostream>

using namespace std;

template <class T>

class Array

{

private:

T *arr;

public:

Array(int c)

{

arr = new T[c];

}

void init(int i, T x)

{

arr[i] = x;

}

T& operator[](int i)

{

return arr[i];

}

};

voidmain()

{

Array<int> array(5);

cout << "input every element"s value:" << endl;

for (int i = 0; i < 5; i++)

{

cin >> array[i];

}

cout << "every element"s value inArray is: " << endl;

for (i = 0; i < 5; i++)

{

cout << "No "<< i <<" : " << array[i]<< endl;

}

}

運行結果:

input every element"s value:

6 21 27 17 40

every element"s value in Array is:

No 0 : 6

No 1 : 21

No 2 : 27

No 3 : 17

No 4 : 40

例二 類模板參數是類

類模板的參數不僅可以是標準數據類型,還可以是用戶自定義類型,當然包括用戶自己定義的類,如下所示:

//**********************

//**** 15_6.cpp****

//**********************

#include<iostream>

using namespace std;

class A

{

private:

int j;

public:

A(){}

A(int x):j(x){}

A(A* x)

{

j = x->j;

}

void operator!()

{

cout << "j = "<< j <<endl;

}

};

template<typenameT>

class B

{

private:

int i;

T* x;

public:

B(int xa, T* p): i(xa)

{

x = new T(p);

}

void operator!()

{

cout << "i = "<< i <<endl;

!*x;

}

};

voidmain()

{

A a(1);

B<A> b(2,&a);

!b;

}

運行結果:

i =2

j =1

例二

//**********************

//**** 15_7.cpp****

//**********************

#include<iostream>

using namespace std;

template<class T>

class Stack

{

public:

Stack(int = 10);//default constructor(stack size 10)

~Stack()

{

delete[]stackPtr;//destructor

}

bool push(const T&);//push an element onto the stack

bool pop(T&);//pop an element off the stack

private:

int size;//# of elements in the stack

int top;//location of the top element

T* stackPtr; //pointer to thestack

bool isEmpty() const

{

return top == -1;

}

bool isFull() const

{

return top == size-1;

}

};

//Constructor with default size 10

template<class T>

Stack<T>::Stack(intS)

{

size = S > 0 ? S : 10;

top = -1;//Stack is initially empty

stackPtr = new T[size];//allocate space for elements

}

//Push an element onto the stack

//return true if successful false otherwise

template <class T>

boolStack<T>::push(constT& pushValue)

{

if (!isFull())

{

stackPtr[++top] = pushValue;//place item in Stafck

return true;//push successful

}

return false;

}

//Pop an element off the stack

template<class T>

boolStack<T>::pop(T&popValue)

{

if (!isEmpty())

{

popValue = stackPtr[top--];//remove item from Stack

return true;//pop successful

}

return false;

}

voidmain()

{

Stack<double>doubleStack(5);

double d = 1.1;

cout << "Pushing elements ontodoubleStack
";

while (doubleStack.push(d))

{

//success true returned

cout << d<< " ";

d += 1.1;

}

cout << "
Stack is full. cannot push" << d

<< "

Poping elements fromdoubleStack
";

while (doubleStack.pop(d))//success true returned

{

cout << d<< " ";

}

cout << "
Stack is empty.Cannotpop
";

Stack<int> intStack;

int i = 1;

cout << "
Pushing elements ontointStack
";

while (intStack.push(i))

{

//success true returned

cout << i<< " ";

++i;

}

cout << "
Stack is full.Cannot push" << i

<< "

Popping elements fromintStack
";

while (intStack.pop(i))//success true returned

{

cout << i<< " ";

}

cout << "
Stack is empty.Cannotpop
";

}

運行結果:

Pushing elements onto doubleStack

1.1 2.2 3.3 4.4 5.5

Stack is full. cannot push 6.6

Poping elements from doubleStack

5.5 4.4 3.3 2.2 1.1

Stack is empty.Cannot pop

Pushing elements onto intStack

1 2 3 4 5 6 7 8 9 10

Stack is full.Cannot push 11

Popping elements from intStack

10 9 8 7 6 5 4 3 2 1

Stack is empty.Cannot pop


推薦閱讀:

sumif 函數
將數據放入代碼中的shellcode函數
ROW函數的用途
Excel函數與公式-1
【Excel函數教程】SUM函數的取代函數SUMPRODUCT

TAG:函數 | 模板 |