繁体   English   中英

用于向量定义的C ++类模板结构

[英]c++ class template structure for vector definition

我对c ++真的很SMatrix<T>(int M) ,我不了解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;}

具有相同的语义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM