C++上三角矩阵括号运算符
C++ Upper Triangular Matrix Bracket Operator
我的某个项目遇到了障碍。我想创建一个上三角矩阵(对角线以下的所有元素都为零),它只分配容纳非零元素所需的内存。我遇到的问题是索引到这个矩阵中。我宁愿不重新设计矩阵,但如果这是唯一的解决方案,那么我可以接受。顺便说一句,Vector是我自己的实现,而不是stl Vector。
到目前为止,我已经将类声明如下:
template <class T>
class UMatrix : public AbstractMatrix<T>
{
private:
Vector<T>* m_data;
int m_size;
public:
//lots of member functions
};
上三角矩阵的构造函数:
template <class T>
UMatrix<T>(const int size)
{
m_size = size;
if(size < 1)
throw(RangeErr(size));
m_data = new Vector<T> [size];
for(int i = 0; i < size; i++)
{
Vector<T> init(i + 1);
m_data[i] = init;
}
}
还有我遇到麻烦的代码:
template <class T>
Vector<T>& operator[] (const int index)
{
if(m_data != NULL && index >= 0 && index < m_rows)
{
// Insert dark magic here
}
else
{
throw(RangeErr(index));
}
}
这导致交错向量的阵列,每个向量比前一个向量大1个长度。
我试图实现括号运算符,以便UMatrix[a][b]
访问真正的上三角矩阵的行a、列b。这意味着CCD_ 2当CCD_。
括号运算符也是来自抽象基类的虚拟函数,并且必须返回Vector&。此外,此实现必须使用括号运算符,而不是函数()运算符,以便与以前编写的代码兼容。我知道使用密集矩阵会更简单,但我仅限于必要的内存使用。
我的第一次尝试是使用一个向量进行存储,类似于1d数组矩阵。然而,括号运算符似乎也不可能为该实现编写。
我的问题是:有可能实现这个括号运算符吗?
好吧,我不知道这是否是可接受的解决方案,因为您没有提供任何关于Vector
类的信息。您可以创建另一个类,让我们称之为SparseVector<U>
,在那里您将获得类似以下的代码
virtual U& operator[](int i) {
if(m_data != nullptr) {
if(m_nnzb <= i && i <= m_nnze) {
return m_data[i - m_nnzb];
}
}
throw std::runtime_error("bad index: in SparseVector::operator[]");
}
这里是这个实现的想法。这是大小为m_size
的向量,其中我们仅存储范围从m_nnzb
到m_nnze
中的非零元素。我们用这个类作为矩阵类中的下标运算符。以下是完整的代码和小示例。
#include <iostream>
#include <exception>
#include <stdexcept>
/* vector interface */
template<typename T>
class IVector {
public:
virtual T& operator[](int i) = 0;
};
/* matrix interface */
template<typename T>
class IMatrix {
public:
virtual IVector<T>& operator[](int i) = 0;
};
/* implementation for upper triangular matrix */
template<typename T>
class UpperMatrix : public IMatrix<T> {
public:
/* implementation for sparse vector */
template<typename U>
class SparseVector : public IVector<U> {
public:
SparseVector() {
m_size = m_nnzb = m_nnze = 0;
m_data = nullptr;
}
SparseVector(int size, int b, int e) {
m_size = size;
m_nnzb = b;
m_nnze = e;
m_data = new U[e - b];
}
SparseVector(const SparseVector<U>& other) {
m_size = other.m_size;
m_nnzb = other.m_nnzb;
m_nnze = other.m_nnze;
m_data = new U[m_nnze - m_nnzb];
for(int i = 0; i < m_nnze - m_nnzb; ++i) {
m_data[i] = other.m_data[i];
}
}
virtual U& operator[](int i) {
if(m_data != nullptr) {
if(m_nnzb <= i && i <= m_nnze) {
return m_data[i - m_nnzb];
}
}
throw std::runtime_error("bad index: in SparseVector::operator[]");
}
protected:
int m_size;
int m_nnzb;
int m_nnze;
U* m_data;
};
UpperMatrix(int n) {
m_size = n;
m_data = new SparseVector<T>[n];
for(int i = 0; i < n; ++i) {
m_data[i] = SparseVector<T>(n, i, n);
}
}
virtual IVector<T>& operator[](int i) {
if(i < m_size && m_data != nullptr) {
return m_data[i];
}
throw std::runtime_error("bad index in UpperMatrix::operator[]");
}
protected:
int m_size;
SparseVector<T>* m_data;
};
int main(int argc, char** argv) {
UpperMatrix<int> m1(3);
/* correct index */
for(int i = 0; i < 3; ++i) {
for(int j = i; j < 3; ++j) {
m1[i][j] = i + j;
}
}
for(int i = 0; i < 3; ++i) {
for(int j = i; j < 3; ++j) {
std::cout << m1[i][j] << " ";
}
std::cout << std::endl;
}
/* incorrect index */
try {
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
m1[i][j] = i + j;
}
}
} catch(const std::exception& ex) {
std::cout << "error occured: " << ex.what() << std::endl;
}
}
教授更改了项目规范以允许括号运算符重载。这是解决这个问题的最佳方法。
相关文章:
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 布尔比较运算符是如何在C++中工作的
- 重载运算符new[]的行为取决于析构函数
- C++上三角矩阵括号运算符