From 765165c1534e7b10b542a6fc07e750b0af382198 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Sat, 12 Mar 2022 12:50:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E7=94=A8=E5=BA=93=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SoftwareDesign/Code/inc/matrix.hpp | 20 ++++++++++++++++++-- SoftwareDesign/Code/inc/vector.hpp | 22 ++++++++++++---------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/SoftwareDesign/Code/inc/matrix.hpp b/SoftwareDesign/Code/inc/matrix.hpp index 9f9d727..93cfd02 100644 --- a/SoftwareDesign/Code/inc/matrix.hpp +++ b/SoftwareDesign/Code/inc/matrix.hpp @@ -2,7 +2,7 @@ * @Author: iR * @Date: 2022-03-11 16:44:46 * @LastEditors: iR - * @LastEditTime: 2022-03-12 12:30:44 + * @LastEditTime: 2022-03-12 12:49:04 * @FilePath: \Code\inc\matrix.hpp * @Description: * @@ -86,7 +86,23 @@ public: * @param {int} h * @return {*} */ - T &at(int w, int h) { return _mat[h * _width + w]; } + T &at(int w, int h) + { + double &ref = _mat->get(h * _width + w); + return ref; + } + + /** + * @description: 只读引用访问 + * @param {int} w + * @param {int} h + * @return {*} + */ + const T &at(int w, int h) const + { + double &ref = _mat->get(h * _width + w); + return ref; + } const int getWidth() const { return _width; } const int getHeight() const { return _height; } diff --git a/SoftwareDesign/Code/inc/vector.hpp b/SoftwareDesign/Code/inc/vector.hpp index dd7bb47..5ec46da 100644 --- a/SoftwareDesign/Code/inc/vector.hpp +++ b/SoftwareDesign/Code/inc/vector.hpp @@ -2,11 +2,11 @@ * @Author: iR * @Date: 2022-03-11 16:47:31 * @LastEditors: iR - * @LastEditTime: 2022-03-12 12:20:35 + * @LastEditTime: 2022-03-12 12:49:25 * @FilePath: \Code\inc\vector.hpp - * @Description: - * - * Copyright (c) 2022 by iR, All Rights Reserved. + * @Description: + * + * Copyright (c) 2022 by iR, All Rights Reserved. */ #ifndef _VECTOR_HPP_ #define _VECTOR_HPP_ @@ -43,10 +43,10 @@ public: ~Vector() { delete[] _v; } //获取值 - T get(int a) const; + T &get(int a); //操作符重载 - T& operator[](int i); + T &operator[](int i); //某处更改为某值 T put(int a, T value); @@ -109,7 +109,7 @@ Vector::Vector(int len, T value) } template -T Vector::get(int a) const +T &Vector::get(int a) { try @@ -124,17 +124,19 @@ T Vector::get(int a) const catch (const char *msg) { std::cout << msg; - return 0; } } template -T& Vector::operator[](int a) +T &Vector::operator[](int a) { try { if (a >= 0 && a < _used) - return _v[a]; + { + T &ref = _v[a]; + return ref; + } else throw "Error: Request vercor data out of range!\n"; }