【问题标题】:Construct an empty matrix [closed]构造一个空矩阵
【发布时间】:2014-12-04 01:44:30
【问题描述】:

我有课

class A
{
    int *const e;
    const int row, column;
public:
    A(int r, int c); // create a empty matrix with r rows and c columns passed in method
}

int main(int argc, char* argv[])
{
    A test(2,2); 
    return 0;
}

问题是如何编写一个构造函数来创建一个我可以使用的矩阵?

【问题讨论】:

  • 如果您想要任何预构建的矩阵功能,请查看 Eigen 等库
  • 但是在这个项目中我不想使用任何库
  • memset 是你的朋友,假设你真的只是想要一个“空”(顺便说一句,它在软件开发中没有实际意义)矩阵。另外,由于empty这个词出现在你的问题标题中,但没有出现在正文中,我不知道你真正想要的是什么。
  • 另外,停止使用rc 等参数名称。我要么 a) 不雇用刚从学校毕业的你,要么 b) 在连续几次代码审查显示变量命名不是你的强项后解雇你。

标签: c++ matrix multidimensional-array


【解决方案1】:

你的构造函数只是

A::A(int r, int c) : row{r}, column{c}
{
    e = new int[r * c];
}

那么你的析构函数应该是

A::~A()
{
    delete[] e;
}

你可以访问元素

for (int i = 0; i < row; ++i)
{
    for (int j = 0; j < column; ++j)
    {
        std::cout << e[i * row + j] << " ";  // Using array arithmetic
    }
    std::cout << std::endl;
}

【讨论】:

  • Error 1 error C2758: 'A::e' : a member of reference type must be initialized 所以我应该先初始化变量吗?
【解决方案2】:

您的问题听起来像: 如何将参数保存到常量行/列属性中。

A::A(int r, int c): row(r), column(c), e(new int[r*c])
{
    for (int i=0; i<r*c; i++) e[i]=0;
}

同样不要忘记析构函数:

virtual ~A(){ delete[] e; };

在构造函数的参数列表中,参数是按声明顺序初始化的,所以不能使用行/列(它们还没有初始化)。

【讨论】:

    【解决方案3】:

    你需要一个指向指针的指针来创建一个[普通的]二维数组 所以

    int ** const e;
    

    那么构造函数可以像这样工作:

    e = (int**) malloc(sizeof(int**) * a);
    for(int i=0;i<a;++i) {
      e[i] = (int*) malloc(sizeof(int*) * b);
    }
    

    您的下一个问题是“如何初始化常量”。为此,您可以在此处参考答案: how to initialize const member variable in a class C++

    要实现这一点,请将初始化程序代码放在一个函数中:

    initConstArray(const int** const a, int r, int  c) {
        e = (int**) malloc(sizeof(int**) * a);
        for(int i=0;i<r;++i) {
          e[i] = (int*) malloc(sizeof(int*) * b);
          for(int j = 0;j < c; ++j) {
            e[i][j] = a[i][j];
        }
    

    并从构造函数初始化列表中调用此函数:

    A(int **a, int r, int c) : initConstArray(a, r, c) {
    }
    

    【讨论】:

    • malloc?我可能会考虑使用硬编码指针而不是智能指针,但这对我来说太不推荐了。
    • 请原谅我的代码。这里只是为了概念性的解释。 - 谢谢
    • 开个玩笑 :-) 但是是的,还有更好的。
    【解决方案4】:

    这是用模板实现矩阵的另一种方法:

    #include <array>
    #include <iostream>
    
    template <std::size_t ROW, std::size_t COLUMN>
    class A
    {
        std::array<int, ROW*COLUMN> a;
    public:
        A():a(){};
    };
    
    
    int main(int argc, char* argv[])
    {
        A<3,3> test; 
        //test.printSize();
        //std::cout << test;
        return 0;
    }
    

    更短更简洁。

    要使注释行起作用,您必须在类中添加以下 2 个函数:

    void printSize(){ std::cout << ROW << "x" << COLUMN << std::endl; };
    
    friend std::ostream& operator<<( std::ostream& os, A<R,C> mat){
        int col_count = 0;
        for (auto id=mat.a.begin(); id!=mat.a.end(); id++){
            os << *id << (++col_count%C==0?"\n":" ");
        }
    
        return os;
    };
    

    【讨论】:

      猜你喜欢
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 2016-03-19
      • 1970-01-01
      • 2014-01-26
      相关资源
      最近更新 更多