简体   繁体   English

C ++ 2D矢量出错

[英]C++ 2D Vector got error

#include <string>
#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

class matrix
{
 public:
    matrix(int);
    void func(int);
    vector<vector<double> > m;
};

matrix::matrix(int size)
{
    //set 3 X 3 = 9 elements to zero
    vector<vector<int> > m(size, vector<int>(size));

    // success
    cout << "test1 if m[0][0] equals to zero: " << m[0][0] << endl;
}

void matrix::func(int size)
{
    // failure, can't pass till here
    cout << "test2 if m[0][0] equals to zero: " << m[0][0] << endl;

    for(int i = 1; i != size; ++i)
    {
        m[i][i] = 0;
    }
}



int main()
{
    int input1 = 3;

    matrix mat(input1);

    mat.func(input1);
}

I want to create a 2D dimensional vector rather using array. 我想创建2D三维矢量而不是使用数组。 However, I got a runtime error. 但是,我遇到了运行时错误。

I create a vector<vector<int> > m(size, vector<int>(size)) to make sure all elements in vector are equal to zero, and I passed the test1. 我创建了一个vector<vector<int> > m(size, vector<int>(size))以确保vector中的所有元素都等于零,并且我通过了test1。 I can't pass test2 on which I commented "failure". 我无法通过评论为“失败”的test2。

Inside your constructor you are creating a local variable m , and use it. 在构造函数内部,您正在创建一个局部变量m ,并使用它。 It is, of course, not available in the func function. 当然,它在func函数中不可用。 To make it work, you instead need to initialize the object's member variable m . 为了使其正常工作,您需要初始化对象的成员变量m Furthermore, the vector you are creating in the constructor is of a wrong type ( int instead of double ) 此外,您在构造函数中创建的向量的类型错误(用int代替double

matrix::matrix(int size)
    : m(size, vector<double>(size)) //initializing the vector
{
}

Suggestion: I can't help thinking that it would be a bit simpler (and possibly internally more compact) if you just stored this internally as one vector rather than a vector of vectors. 建议:我不禁想到,如果仅将其内部存储为一个向量而不是向量的向量,则它会更简单(并且可能在内部更紧凑)。 Ie

std::vector<double> m;
size_t x;
size_t y;

This might not seem two dimensional but remember that this is a private detail of your class and that's what encapsulation is for. 这似乎不是二维的,但请记住,这是您的类的私有细节,这就是封装的目的。

I think the problem you're getting here though is that you're trying to deference the vector(s) before having sized them. 认为您到达这里的问题是,在调整大小之前,您试图参考向量。 When you create a vector you either have to give it an initial size, or you push values back on to it to increase the size. 创建矢量时,您要么必须给它一个初始大小,要么将值推回去以增加大小。 You probably want to do the former which you could do with: 您可能想做一个可以做的前者:

void matrix::resize( size_t x1, size_t y1 )
{
    m.clear();
    x = x1;
    y = y1;
    m.resize( x * y );
}

You can then access an individual element of the matrix using: 然后,您可以使用以下命令访问矩阵的单个元素:

double matrix::get( size_t x1, size_t y1 )
{
    return m[ x1 * x + y1 ];
}

Basically, what you're doing in the constructor is 基本上,您在构造函数中所做的是

vector<vector<int> > m(size, vector<int>(size));

m is a local variable here. m是一个局部变量。 Hence, it is not available outside the constructor, and hence in the func() , you are basically trying to access an uninitialised vector. 因此,它在构造函数外部不可用,因此在func() ,您基本上是在尝试访问未初始化的向量。

To fix this, initialise m in the constructor by changing the above statement to: 要解决此问题,请通过将上述语句更改为在构造函数中初始化m

m = vector<vector<int> >(size, vector<int>(size));

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

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