对称矩阵的C++容器和==运算符
C++ container and == operator for symmetric matrix
以下是我处理对称矩阵的部分代码:
SymmetricMatrix<std::deque<int> > a(3);
SymmetricMatrix<std::list<int> > b(3);
SymmetricMatrix<std::vector<int> > c(4);
SymmetricMatrix<std::list<int> > d(3);
a(1,1) = b(1,1) = c(1,1) = d(1,1) = 7;
a(1,2) = b(2,1) = c(1,2) = d(2,2) = 3;
if(b == c) {}
您建议使用哪种容器或数据结构来实现SymmetricMatrix
类?(我尝试过std::vector
)此外,我如何实现==运算符?现在,它适用于b==d,但不适用于a==b(no match for 'operator =='
)。
==
在a == b
上未定义的原因是因为类SymmetricMatrix<std::deque<int> >
和SymmetricMatrix<std::list<int> >
的类型不同。其中一个内部定义的运算符==
将不适用。
然而,C++允许您定义"独立的"操作员模板,如以下所示:
template <typename T, typename U>
bool operator==(const SymmetricMatrix<T>& lhs, const SymmetricMatrix<U>& rhs) {
// Do the comparison here
}
此运算符引用具有不同类型参数的SymmetricMatrix
对象,因此编译器将能够为a == b
调用它。您可能需要在SymmetricMatrix
模板中向该运算符声明"友谊",以便该运算符访问矩阵类的私有和受保护成员。
相关文章:
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 布尔比较运算符是如何在C++中工作的
- 重载运算符new[]的行为取决于析构函数
- 如何防止clang格式在流运算符调用之间添加换行符<<