矢量定义的C 类模板结构

c++ class template structure for vector definition

本文关键字:结构 定义      更新时间:2023-10-16

我真的是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;}

具有相同的语义。