c++模板

主要設計函數模板和類模板

函數模板:

template <typename T1[, typename T2, ...]> 返回類型 函數名(參數列表)

其中,參數列表可以不同,此時爲重載

示例:

#include <iostream>
#include<stdlib.h>
#include <string>
using namespace std;

template <typename T> T Max(T a, T b) {
	return a < b ? b : a;
}

int main()
{
	

	int aa = 6, bb = 7;
	double da = 6.0, db = 7.0;
	cout << Max<int>(aa, bb) << endl;
	cout << Max(aa, bb) << endl;
	cout << Max(da, db) << endl;


	system("pause");
	return 0;
}

#include <iostream>
#include<stdlib.h>
#include <string>
using namespace std;


template<typename T1, typename T2> T1 add(T1 a, T2 b) {
	return a + b;
}
int main()
{
	

	int aa = 6, bb = 7;
	double da = 6.1, db = 7.0;
	

	cout << add(aa, bb) << endl;
	cout << add<double, int>(da, aa) << endl;
	cout << add(da, aa) << endl;
	cout << add(aa, da) << endl;
	system("pause");
	return 0;
}

out:

13
12.1
12.1
12
請按任意鍵繼續. . .

warning C4244: “return”: 從“T2”轉換到“T1”,可能丟失數據,按位置指定的。


#include <iostream>
#include<stdlib.h>
#include <string>
#include<iomanip>
using namespace std;


template <typename T1, typename T2> void inverse(T1 *mat1, T2 *mat2, int a, int b) {
	int i, j;
	for(i=0;i<a;i++)
		for (j = 0;j < b; j++)
			mat2[j][i] = mat1[i][j];
	
}

template <typename T> void inverse_(T ** mat1, T **mat2, int a, int b) {
	int i, j;
	for (i = 0; i <a; i++)
		for (j = 0;j <b; j++)
			mat2[j][i] = mat1[i][j];
		
}

template <typename T> void output(T *mat, int a,int b) {
	int i, j;
	for (i = 0; i < a; i++) {	
		for (j = 0; j < b; j++) {
			cout <<setw(6)<< mat[i][j];
		}
		cout << endl;
	}
	cout << endl;

}

int main()
{
	

	int middle[6][3];
	int middle_[6][3];
	int matrix[3][6] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 };

	output(matrix, 3, 6);

	inverse(matrix, middle,  3, 6);//顯示: inverse<int[6], int[3]>(matrix, middle,  3, 6)
	output(middle, 6,3);
	
	//inverse_(matrix, middle_, 3, 6); 這是錯誤定義
	//output(middle_, 6, 3);
	system("pause");
	return 0;
}

類模板

template <typename T1[, typename T2, ...]> class 類名{
//類定義

//成員函數
返回類型 函數名(參數列表);
};

//類成員函數
template<模板參數列表> 返回類型 類名<模板參數列表> :: 成員函數1(參數列表){
}
#include <iostream>
#include<stdlib.h>
#include <string>
#include<iomanip>
using namespace std;

//可以增加默認值的
template <typename T=int, size_t s = 5> class Array {
private:
	T arr[s];
public:
	T& operator[](const size_t &num) {
		if (num >= s) {
			cout << "訪問越界!!返回數組第一個值" << endl;
			return arr[0];

		}
		return arr[num];
	}

	



};



int main()
{
	
	const int row = 3, col = 4;
	Array<Array<int, col>, row> arr;
	
	/*數組賦值 和 打印數組*/
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			arr[i][j] = i + j;
		}
	}
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			cout <<setw(3)<< arr[i][j];
		}
		cout << endl;
	}
	 

	cout << arr[4][1];//輸出1  注意一下
	cout << arr[2][6];// 輸出2  注意一下

	
	system("pause");
	return 0;
}

#include <iostream>
#include<stdlib.h>
#include <string>
#include<iomanip>
using namespace std;

//可以增加默認值的
template <typename T=int, size_t s = 5> class Array {
private:
	T arr[s];
public:
	T& operator[](const size_t &num) {
		if (num >= s) {
			cout << "訪問越界!!返回數組第一個值" << endl;
			return arr[0];

		}
		return arr[num];
	}

	T max();

	



};

template <typename T, size_t s> T Array<T, s>::max() {
	T maxv = arr[0];
	int i = 1;
	for (; i < s; i++) {
		if (maxv < arr[i])
			maxv = arr[i];
	}
	return maxv;
}


int main()
{
	
	Array<int, 5> arri;
	Array<double, 7> arrd;

	for (int i = 0; i < 5; i++) {
		arri[i] = i;
	}

	for (int i = 0; i < 7; i++) {
		arrd[i] = (i + 0.1);
	}

	cout << endl << arri.max() << endl;
	for (int i = 0; i < 5; i++) {
		cout<<arri[i]<<' ';
	}

	cout << endl << arrd.max() << endl;
	for (int i = 0; i < 7; i++) {
		cout << arrd[i]<<' ';
	}

	
	

	
	system("pause");
	return 0;
}

out:

4
0 1 2 3 4
6.1
0.1 1.1 2.1 3.1 4.1 5.1 6.1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章