简体   繁体   English

将可变大小数组声明为类/模板类成员变量

[英]Declaring a variable size array as a class/template class member variable

I am trying to write a class in c++ which will contain a 2D array member variable.我正在尝试用 C++ 编写一个包含二维数组成员变量的类。 The point of the class is to represent a simulated physical model using this array, and I intended to write the various actions I want to perform on the array as member functions.这个类的重点是使用这个数组来表示一个模拟的物理模型,我打算把我想在数组上执行的各种操作写成成员函数。

My problem is trying to initialise the array in the class without hard coding it- ideally, I would like to write the overall program so that the dimension of the array can be inputted by the user at the run time, and this can then be used in the overload constructor for the class.我的问题是尝试在类中初始化数组而不进行硬编码 - 理想情况下,我想编写整个程序,以便用户可以在运行时输入数组的维数,然后可以使用在类的重载构造函数中。 ie something like this:即这样的事情:


class Lattice{
public:
    //Overload constructor
    Lattice(int);
private:
    //variables:
    int DimensionSize;
    int lattice[DimensionSize][DimensionSize];
}

Lattice::Lattice(int N){
    DimensionSize = N;
    lattice = new int[DimensionSize][DimensionSize];
}

I can see that the code above clearly won't work, as the variable "DimensionSize" is unspecified at runtime, which means the amount of memory required for the 2d array "lattice" is unknown.我可以看到上面的代码显然不起作用,因为变量“DimensionSize”在运行时未指定,这意味着二维数组“lattice”所需的内存量未知。

I've been looking around on this forum for an answer for this but there doesn't seem to be anything directly helpful for my issue, and any other questions that have been asked seem quite old.我一直在这个论坛上四处寻找答案,但似乎没有任何对我的问题有直接帮助的问题,并且提出的任何其他问题似乎都很古老。 If anyone could point me towards a solution, or let me know if what I want is even possible it would be greatly appreciated- I wonder if using a vector instead of an array may be a solution?如果有人可以指出我的解决方案,或者让我知道我想要的东西是否可能,我将不胜感激 - 我想知道使用向量而不是数组是否可能是一个解决方案?

Update:更新:

I was able to produce the behaviour that I wanted by using a template class:我能够通过使用模板类产生我想要的行为:

template <int N>
class Lattice
{
public:
    //Constructor
    Lattice();
    ~ some other functions ~

private:
    float lattice[N][N];
};

template <int N>
Lattice<N>::Lattice() {
    double rando;
//    Initialise the array to a random state.
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++){
            rando = (double)rand() / ((double)RAND_MAX + 1);
            if( rando <= 0.5 ){
                lattice[i][j] = -1.0;
            }
            else if( rando >= 0.5 ){
                lattice[i][j] = 1.0;
            }
        }
    }
}

I can see using vectors instead of arrays is possible, so my question now is there any major downsides to using a template as above?我可以看到使用向量而不是数组是可能的,所以我现在的问题是使用上述模板有什么主要缺点吗? I can see that the class declaration and definition now have to be contained in the same header file, as outlined in this thread Why can templates only be implemented in the header file?我可以看到类声明和定义现在必须包含在同一个头文件中,如该线程中所述为什么模板只能在头文件中实现? but are there any other problems that may arise apart from this inconvenience?但是,除了这种不便之外,还有其他可能出现的问题吗?

As stated in the comments, if you want to declare a 2D-array and then allocate it with new , declare it like this: int** lattice .如评论中所述,如果您想声明一个 2D 数组,然后使用new分配它,请像这样声明: int** lattice Also, i'd recommend declaring it as a single array (or vector), so that all elements are contigous in memory, and then calculate the element-position using x, y and DimensionSize:此外,我建议将其声明为单个数组(或向量),以便所有元素在内存中都是连续的,然后使用 x、y 和 DimensionSize 计算元素位置:

class Lattice{
public:
    //Overload constructor
    Lattice(int);
    //access-method
    int getElement(int x, int y)
private:
    //variables:
    int DimensionSize;
    //simple pointer
    int* lattice;
}

Lattice::Lattice(int N){
    DimensionSize = N;
    lattice = new int[DimensionSize*DimensionSize];
}

int Lattice::getElement(int x, int y)
{
    \\calculate the element-position from x and y using DimensionSize
    return lattice[x + y*DimensionSize]; 
}

Since lattice is a private member, you'll have to define an access-method anyways, and having all elements contigous in memory can speed up access quite a bit.由于lattice是一个私有成员,您无论如何都必须定义一个访问方法,并且将所有元素在内存中连续放置可以大大加快访问速度。

Edit: here is the code with an explicit 2d-array:编辑:这是带有显式二维数组的代码:

class Lattice{
public:
    //Overload constructor
    Lattice(int);
    //access-method
    int getElement(int x, int y)
private:
    //variables:
    int DimensionSize;
    //simple pointer
    int** lattice;
}

Lattice::Lattice(int N){
    DimensionSize = N;
    lattice = new int[DimensionSize][DimensionSize];
}

int Lattice::getElement(int x, int y)
{
    return lattice[x][y]; 
}

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

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