日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++模板类示例

發布時間:2023/12/9 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++模板类示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

記錄下一道簡單題目的實現:將一個二維矩陣A轉換為B,B[i][j]的值用A中i行的最大值和j列的最小值的平均值為替換。

解題思路很簡單,求一下每行的最大值和每列的最小值,分別存起來。接下來就是求平均數了。

?

為了練習下模板,寫了個模板類記錄下(主要知識點在代碼中有注釋)!

開發環境:vc2012和Fedora20

?

模板類定義及實現Matrix.h:

/*寫模板類注意事項:1. 模板類的聲明與定義(實現)必須放在同一文件中, 若分開在實例化時可能會找不到其定義2. 模板特化可以與聲明分開 */#ifndef __MATRIX_H__ #define __MATRIX_H__#include <iostream> #include <stddef.h> #include <assert.h>using namespace std;// --------------------- 模板類聲明 ------------------- template <typename T> class CMatrix // 二維矩陣模板類 { public:CMatrix(int r = 0, int c = 0){rows = r;cols = c;arr = NULL;if(rows > 0 && cols > 0){arr = new T*[rows]; // 生成指針數組(長度為rows)assert(arr != NULL);for(int i = 0; i < rows; i++)arr[i] = new T[cols]; // 為每個指針分配空間(長度為cols)}}~CMatrix(){if(arr){for(int i = 0; i < rows; i++){if(arr[i])delete arr[i]; // 釋放每一行向量}delete [] arr; // 釋放指針數組}}T getMaxInRow(int r) const // 求二維數組r行中的最大值{T max = arr[r][0]; // 取行中第一個元素for(int i = 1; i < cols; i++){if(max < arr[r][i])max = arr[r][i];}return max;}T getMinInCol(int c) const // 求二維數組c列中的最小值{T min = arr[0][c]; // 取列中第一個元素for(int i = 1; i < rows; i++){if(min > arr[i][c])min = arr[i][c];}return min;}bool setValue(int i, int j, T v);void display();public:int rows;int cols;T** arr; };template <typename T> class CMatrixChanger { public:// 將matIn聲明為const后, 將只能訪問matIn中的const成員(變量或函數)static void Convert(const CMatrix<T>& matIn, CMatrix<T>& matOut); };// ------------------------ 模板類定義(實現) ---------------- template <typename T> bool CMatrix<T>::setValue(int i, int j, T v) {if(i < rows && j < cols){arr[i][j] = v;return true;}return false; }template <typename T> void CMatrix<T>::display() {for(int i = 0; i < rows; i++){for(int j = 0; j < cols; j++)cout << arr[i][j] << '\t';cout << endl;} }/*功能: 矩陣轉換(規則: 求矩陣A[][]中i行中最大值與j列中最小值的平均值), 將結果存放到matOut.arr[][] */ template <typename T> void CMatrixChanger<T>::Convert(const CMatrix<T>& matIn, CMatrix<T>& matOut) {int nr = matIn.rows;int nc = matIn.cols;T* r_max = new T[nr]; // 保存每一行的最大值T* c_min = new T[nc]; // 保存每一列的最小值int i, j;for(i = 0; i < nr; i++){r_max[i] = matIn.getMaxInRow(i);}for(i = 0; i < nc; i++){c_min[i] = matIn.getMinInCol(i);}for(i = 0; i < nr; i++){for(j = 0; j < nc; j++){matOut.arr[i][j] = (r_max[i] + c_min[j]) / 2.0; // 取所在行最大值與所在列最小值的平均值}}delete [] r_max;delete [] c_min; }#endif


主函數來調用下main.cpp:

#include <stdlib.h> #include <time.h> #include "Matrix.h"#define ROWS 4 #define COLS 5void test() {CMatrix<double> matIn(ROWS, COLS), matOut(ROWS, COLS);srand((unsigned)time(NULL)); // 初始化隨機數種子for(int i = 0; i < ROWS; i++) {for(int j = 0; j < COLS; j++){double v = rand() % 320; // 產生一個[0-320)之間的隨機整數來初始化二維數組matIn.setValue(i, j, v);}}matIn.display();CMatrixChanger<double>::Convert(matIn, matOut);cout << endl;matOut.display(); }int main() {test();return 0; }


編譯及運行(取自Fedora20):

[zcm@matrix #293]$make g++ -c -o main.o main.cpp g++ -o main main.o -Wall -Os [zcm@matrix #294]$./main 228 83 263 173 93 152 73 318 160 23 207 292 259 250 314 241 46 171 72 134207.5 154.5 217 167.5 143 235 182 244.5 195 170.5 233 180 242.5 193 168.5 196.5 143.5 206 156.5 132 [zcm@matrix #295]$


?

?

總結

以上是生活随笔為你收集整理的C++模板类示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。