引用库更新

This commit is contained in:
iridiumR 2022-03-12 14:14:15 +08:00
parent 959685868b
commit 1722bc6cfe
2 changed files with 74 additions and 6 deletions

View file

@ -2,7 +2,7 @@
* @Author: iR * @Author: iR
* @Date: 2022-03-11 16:44:46 * @Date: 2022-03-11 16:44:46
* @LastEditors: iR * @LastEditors: iR
* @LastEditTime: 2022-03-12 12:49:04 * @LastEditTime: 2022-03-12 14:11:55
* @FilePath: \Code\inc\matrix.hpp * @FilePath: \Code\inc\matrix.hpp
* @Description: * @Description:
* *
@ -71,15 +71,31 @@ public:
const T get(int w, int h) const; const T get(int w, int h) const;
/** /**
* @description: * @description: []
*/ */
const T operator[](int i) const { return _mat->get(i); } const T operator[](int i) const { return _mat->get(i); }
/** /**
* @description: * @description: =
*/ */
T operator=(const Matrix &b); T operator=(const Matrix &b);
/**
* @description: +=
* @note: a += b*this即为a
* @param {*}
* @return None
*/
void operator+=(const Matrix &b);
/**
* @description: +=
* @note: c = a + b*this即为ac
* @param {*}
* @return None
*/
Matrix operator+(const Matrix &b) const;
/** /**
* @description: 访 * @description: 访
* @param {int} w * @param {int} w
@ -138,7 +154,7 @@ Matrix<T>::Matrix(int w, int h, const T elements[])
int size = w * h; int size = w * h;
try try
{ {
//实际上似乎并不能在这里判断数组是否合法
// if (w * h > size) // if (w * h > size)
// throw "Error: The number of array elements is less than the matrix elements.\n"; // throw "Error: The number of array elements is less than the matrix elements.\n";
// if (w * h < size) // if (w * h < size)
@ -193,7 +209,58 @@ T Matrix<T>::operator=(const Matrix &another)
template <class T> template <class T>
const T Matrix<T>::get(int w, int h) const const T Matrix<T>::get(int w, int h) const
{ {
return _mat[h * _width + w]; return _mat->get(h * _width + w);
}
template <class T>
void Matrix<T>::operator+=(const Matrix &b)
{
try
{
if (this->_height == b.getHeight() && this->_width == b.getWidth())
{
for (int i = 0; i < _width * _height; i++)
this->_mat->put(i, (*this)[i] + b[i]);
return;
}
throw "Error: Different size.";
}
catch (const char *msg)
{
std::cout << msg;
}
}
/**
* @description: +=
* @note: c = a + b*this即为ac
* @param {*}
* @return None
*/
template <class T>
Matrix<T> Matrix<T>::operator+(const Matrix &b) const
{
int h = this->_height;
int w = this->_width;
try
{
if (h == b.getHeight() && w == b.getWidth())
{
Matrix<T> temp(w, h, T(0));
temp += *this;
temp += b;
return temp;
}
throw "Error: Different size.";
}
catch (const char *msg)
{
std::cout << msg;
Matrix<T> temp(w, h, T(0));
return temp;
}
} }
#endif #endif

View file

@ -2,7 +2,7 @@
* @Author: iR * @Author: iR
* @Date: 2022-03-11 16:47:31 * @Date: 2022-03-11 16:47:31
* @LastEditors: iR * @LastEditors: iR
* @LastEditTime: 2022-03-12 12:49:25 * @LastEditTime: 2022-03-12 14:08:48
* @FilePath: \Code\inc\vector.hpp * @FilePath: \Code\inc\vector.hpp
* @Description: * @Description:
* *
@ -124,6 +124,7 @@ T &Vector<T>::get(int a)
catch (const char *msg) catch (const char *msg)
{ {
std::cout << msg; std::cout << msg;
return _v[0];
} }
} }