C++ 模板函数

来源:互联网 发布:linux配置ntp时间同步 编辑:程序博客网 时间:2024/05/29 09:51
#include <cstdio>#include <iostream>#include <string>#include <iomanip>using namespace std;template<typename T1, typename T2> void inverse(T1 *a , T2 *b, int row, int col);template<typename T1> void output(T1 *a, int row, int col);template<typename T> void initArr(T *a, int row, int col);int main(){int a[4][5];initArr<int[5]> (a, 4, 5);cout<< "正矩阵 :" << endl;output<int[5]> (a, 4, 5); //显示声明 模板的类型信息int b[5][4];    inverse<int[5],int[4] > (a, b, 4, 5);cout<< "反转矩阵:"<< endl;output<int[4]> (b, 5, 4);return 0;};template<typename T1, typename T2> void inverse(T1 *a, T2 *b, int row , int col ){for (int i = 0; i < row; i ++){for (int j = 0; j < col; j++){b[j][i] = a[i][j];}}}template<typename T1> void output(T1 *a, int row, int col) {for (int i = 0; i < row; i++){for (int j =0; j < col; j++){cout << setw(6) << a[i][j];//setw 设置 输出占的宽度}cout<<endl;}}template<typename T> void initArr(T *a, int row, int col){int num = 0;for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){a[i][j] = (num++);}}}

0 0
原创粉丝点击