简体   繁体   English

C ++使用Const int定义数组

[英]C++ Define Array with Const int

I'm just learning about C++. 我只是在学习C ++。 I'm doing practice with initialize list, so I made a class like this 我正在用初始化列表进行练习,所以我做了一个这样的课

class Matrix
{
    public:
        const int x_size;
        const int y_size;
        int *data;

        Matrix(int _x_size, int _y_size) : x_size(_x_size), y_size(_y_size)
        {
            data = new int[y_size][x_size];
        }

        ~Matrix()
        {
            delete[][] data;
        }
};

int main(void)
{
    Matrix A = Matrix(10, 10);
    return 0;
}

And compiler said as: array size in operator new must be constant. 并且编译器表示为:运算符new中的数组大小必须恒定。 So I searched and someone said, these are not 'compiler time constant'. 因此,我进行了搜索,有人说,这些不是“编译器时间常数”。

But it is obvious that I can't use that size as macros in here... Then. 但是很明显,我不能在此处使用该大小作为宏...然后。 How should I get proper-sized array with Constructor? 如何使用Constructor获得适当大小的数组?

If you are just learning c++ then the best tip is stay away from memory management. 如果您只是在学习c ++,那么最好的技巧就是远离内存管理。 Use the stl types if you can. 如果可以,请使用stl类型。 Use a std::vector to replace that array: 使用std :: vector替换该数组:

std::vector<std::vector<int>> data;

create it like this: 像这样创建它:

data(y_size, std::vector<int>(x_size, 0));

and access it like this: 并像这样访问它:

data[i][j];

As jaunchopanza said, you can also use a 1D array which might be better. 正如jaunchopanza所说,您也可以使用一维数组,这可能更好。 You would create and edit it in similar ways: 您将以类似的方式创建和编辑它:

std::vector<int> data;
data(y_size * x_size, 0);
data[y_size*i + j];

The advantage is that it is faster for accessing, especially if x_size and y_size are going to be large. 优点是访问速度更快,尤其是x_sizey_size较大时。 There is also an advantage in that the vector of vectors may be stored all over the place, as in each row(or column) will be in different places in memory. 还有一个优点是向量的向量可以存储在整个位置,因为每一行(或列)将在内存中的不同位置。 If you intend to get data that overlaps more than one row (or column) then it would be better to use the 1D array for speed. 如果您打算获取重叠超过一行(或一列)的数据,则最好使用1D数组来提高速度。

You can Find more info here: http://en.cppreference.com/w/cpp/container/vector 您可以在此处找到更多信息: http : //en.cppreference.com/w/cpp/container/vector

If you want to turn this into a matrix and do math etc. Than I would highly recomend Eigen it is by far the best matrix library: http://eigen.tuxfamily.org/index.php?title=Main_Page 如果您想将其转换为矩阵并进行数学运算,那么,比起我本人,我强烈推荐Eigen它是迄今为止最好的矩阵库: http ://eigen.tuxfamily.org/index.php? Eigen = Main_Page

you should write sth like this ( although it is not exception safe) 您应该这样写某事(尽管它不是异常安全的)

class Matrix
{
    public:
        const int x_size;
        const int y_size;
        int ** data;

        Matrix(int _x_size, int _y_size) : x_size(_x_size), y_size(_y_size)
        {
            data = new int* [y_size];
            for(int i =0 ; i < y_size ; i++)
            {
                data[i] = new int[x_size] ;
            }
        }

        ~Matrix()
        {
            for(int i= 0 ; i < y_size ; i++)
            {
                delete [] data[i];
            }
            delete[] data;
        }
};

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

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