矢量定义的C 类模板结构
c++ class template structure for vector definition
我真的是C 的新手,我不明白SMatrix<T>(int M)
在程序中正在做什么。我已经在类模板中搜索了很多有关构造函数的信息,但这不是语法。据知道,sMatrix<T>
是数据类型。以下是代码。
#ifndef T_SIMPLE_MATRIX_H
#define T_SIMPLE_MATRIX_H
#include <vector>
template<class T>
class SMatrix {
private:
// Storage
std::vector<T> v;
int N;
public:
SMatrix<T>(int M){v.resize(M*M);N = M;}
SMatrix<T>(){N = 0;}
void resize(int M){
v.resize(M*M);
N = M;
}
int size(){
return N;
}
// access matrix(r, c) (row and column) for R/W operations
T& operator() (int r, int c){
return v[N*r+c];
}
const T& operator()(int r, int c) const{
return v[N*r+c];
}
};
#endif //
在类模板中, SMatrix<T>
和 SMatrix
表示同一件事。
您可以并且应该使用
SMatrix(int M){v.resize(M*M);N = M;}
SMatrix(){N = 0;}
具有相同的语义。
相关文章:
- 我的超类中的模板问题与结构定义
- 为私有结构定义双参数运算符重载
- 可视化C++中的结构定义错误
- 如何在结构定义中组合__attribute__((dllexport))和[[nodiscard]]?
- COM 互操作结构定义与内存布局不匹配
- C 在标题文件中创建具有结构定义的结构的队列
- 如何将结构定义为结构的成员
- 在这个节点定义中,为什么我们在结构定义之后使用"Node"?
- 不同模块中的ambigus结构定义
- 有条件的C/C 结构定义
- C++ 初始化结构定义中的向量
- 字符串声明中的结构定义中的字符串声明
- 使用结构定义函数
- 为具有许多数据成员的结构定义 == 和<
- enableif结构定义和默认模板参数
- C 使用模板为结构定义别名
- C/C++ 中的套接字编程 - 接收由 typedef 结构定义的消息
- 为C++结构定义显式析构函数如何影响调用约定
- 当结构定义在头文件中时,如何在main()中创建结构数组
- C结构定义标准