简体   繁体   English

如何初始化int * const * const?

[英]How to initialize int *const *const?

I need a 2d array with fixed width and height that can only change the individual values stored in it. 我需要一个具有固定宽度和高度的2d数组,该数组只能更改存储在其中的单个值。 It is declared in a header and later initialized in a source file. 它在标头中声明,然后在源文件中初始化。

What I found made me try the following snippets; 我发现,我尝试了以下片段; unfortunately questions were about either 1d or non-const arrays and did not match my situation. 不幸的是,有关1d或非const数组的问题与我的情况不符。

int *const *const a = new int[10][10];
int *const *const b = new int[10][10]();
int *const *const c = new int*[10];
for (int i = 0; i < 10; ++i) {
    c[i] = new int[10];
}

My hope was in the last example, but how can I use the "inner" arrays of c if they are not initialized and I am not able to initialize them since they are const? 我的希望是在最后一个示例中,但是如果未初始化 c的“内部”数组, 由于它们是const 而无法初始化,该如何使用它们呢?

Do I not need a different type for this array? 我不需要此数组的其他类型吗? I was thinking about int d[][] but it doesn't have constant width and height. 我在考虑int d[][]但宽度和高度不是恒定的。
It seems to me like a paradox (if it exists in the c++ world), am I missing something? 在我看来,这就像一个悖论(如果它存在于c ++世界中),我是否缺少某些东西?

I was thinking about int d[][] but it doesn't have constant width and height. 我在考虑int d[][]但宽度和高度不是恒定的。

int d[][] does not make sense (and will be rejected by the compiler). int d[][]没有任何意义(并且将被编译器拒绝)。 As far as multi-dimensional arrays are concerned, only the first dimension's size can be omitted to denote an incomplete type. 就多维数组而言,仅第一维的大小可以省略以表示不完整的类型。 The other dimensions' sizes are part of the type. 其他尺寸的尺寸是该类型的一部分。 You cannot omit them, much like you cannot omit the int . 您不能忽略它们,就像您不能忽略int

In other words, if you have something like int d[5][10] , then you can think of it as a one-dimensional array of element type int[10] . 换句话说,如果您拥有int d[5][10] ,则可以将其视为元素类型为int[10]的一维数组。 Generally, think of multi-dimensional arrays as a special case of one-dimensional arrays . 通常,将多维数组视为一维数组的特例 It will make everything easier to understand. 这将使所有内容更容易理解。


The best solution to your problem in C++ is to create a class with private std::array<T, Width * Height> data; 用C ++解决问题的最佳方法是使用私有std::array<T, Width * Height> data;创建一个类std::array<T, Width * Height> data; and int width member variables inside, and calculate the array offset from individual x and y arguments in a public member function, for example: int width成员变量,并从公共成员函数中的各个x和y参数计算数组偏移量,例如:

T& operator()(int x, int y)
{
    return data[y * width + x];
}

If the dimensions are only known at run-time, then the only thing you have to change is using std::vector instead of std::array . 如果只有在运行时才知道尺寸,那么您唯一需要更改的就是使用std::vector而不是std::array Storage will still be contiguous. 存储仍将是连续的。

Here is a complete example: 这是一个完整的示例:

#include <vector>
#include <iostream>

class Matrix
{
public:
    Matrix(int width, int height, int value) :
        width(width),
        data(width * height, value)
    {}

    int& operator()(int x, int y)
    {
        return data[y * width + x];
    }

private:
    int width;
    std::vector<int> data;
};

int main()
{
    Matrix m(5, 10, 123);
    std::cout << m(7, 8) << "\n";
    m(7, 8) = 124;
    std::cout << m(7, 8) << "\n";
}

My hope was in the last example, but how can I use the "inner" arrays of c if they are not initialized and I am not able to initialize them since they are const ? 我的希望是在最后一个示例中,但是,如果未初始化c的“内部”数组,并且由于它们是const将无法初始化它们,该如何使用?

That's not really true at all: 这根本不是真的:

int * const * const c = new int*[10]
{
    new int[10], new int[10], new int[10], new int[10], new int[10],
    new int[10], new int[10], new int[10], new int[10], new int[10]
};

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

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