简体   繁体   English

C ++中的动态2D数组作为参数

[英]Dynamic 2D arrays in C++ as parameters

I have a 2D array of doubles for a problem using dynamic programming. 对于使用动态编程的问题,我有一个二维的双精度数组。 What I want to do is below (f is any function operating on the variable "size" and returns an int) : 我想做的如下(f是对变量“ size”进行操作的任何函数并返回一个int值):

void myFunction(int size){
    double myArr[size][f(size)];
    helperFunction(size, myArr);
}

void helperFunction(int size, double[][] myArr){
    //do something
}

However this code is obviously a mashup of java and c++. 但是,此代码显然是Java和C ++的混搭。 How would I accomplish this in c++? 我将如何在C ++中完成此任务?

Highly recommend using vectors as alternative to dynamic arrays. 强烈建议使用向量替代动态数组。 Vectors have a bad reputation in java, but in c++ they're the way to go. 向量在Java中的声誉很差,但是在c ++中,它们是必经之路。 Demonstrating "a" c++ way of accomplishing this, using both "new" allocation and vectors. 使用“新”分配和向量来演示实现此目标的“一种” c ++方法。 Warning: unlike java, primitive arrays in c++ will require you to keep track of array sizes; 警告:与Java不同,C ++中的原始数组将要求您跟踪数组大小; all inner arrays as well. 所有内部数组也是如此。

#include <iostream>
#include <vector>

class Test{
private:
    double **myArray;//Primitive 2d array; suggest using 2d vector.
    std::vector<std::vector<double>> myVector;//Alternative vector.
    std::vector<int> col;//Using vector for myArray sizes; where sizes = inner array size.
    int size;//Required with primitive dynamic array; size of array.
public:
    Test(){
        myArray = 0;//c++ initialization before reading; safety.
        size = 0;//c++ "" "" ""
    }
    ~Test(){
        flushArray();//Flush all data on destructor.
    }
    void flushArray(){//Empties 2d array and set it to 0.
        //Flush of primitive 2d array.
        for(int i = 0; i < size; ++i) {//Needs to delete every new data created
            delete []myArray[i];//Freeing memory for inner node.
        }
        delete []myArray;//Freeing memory for outer node.
        myArray = 0;//Setting pointer to 0;
        size = 0;//Setting size to 0;
        col.clear();//Flush for column vector: easy.

        //vector is self maintained and will clear itself on destructor; exception: "new" DATA.
    }
    void myFunction(int size) {
        if(this->size != 0) {//If there is already data...
            flushArray();//Flush Array;
        }
        this->size = size;//Require size to free during next call.
        myArray = new double*[size];//Create new array of nothings with a size of "size".
        for(int i = 0; i < size; ++i) {
            //Traversing through array and adding an array of doubles.
            myArray[i] = new double[f(size,false)];//New DATA can be implicit
        }
    }
    void otherFunction(int size) {
        myVector.clear();//Flush Vector;
        myVector.resize(size);//Automated dynamic sizing
        for(auto it = myVector.begin(); it != myVector.end(); ++it) {
            it->resize(f(size,true));
        }
    }
    int f(int size, bool isVector) {
        //.., do something.
        if(isVector) {
            return size;//Whatever int you were meant to return.
        }
        //Keep track of cols, maybe they'll vary
        col.push_back(size);//it might be (size+i)... Required for dynamic array.
        return col.back();//Return the intended size.
    }
    void printArraySize() {
        for(int i = 0; i < size; ++i) {
            std::cout<<"myArray["<<i<<"] has "<<col[i]<<" elements."<<std::endl;
        }
    }
    void printVectorSize() {
        //Using a counter, chose to use primitive for-loop.
        for(int i = 0; i < myVector.size(); ++i) {
            std::cout<<"myVector["<<i<<"] has "<<myVector[i].size()<<" elements. "<<std::endl;
        }
    }
};
int main()
{
    Test test;

    test.myFuntion(10);
    test.otherFunction(10);

    test.printArraySize();
    test.printVectorSize();

    return 0;
}

Printing results to show size: 打印结果以显示尺寸:

myArray[0] has 10 elements.  
myArray[1] has 10 elements.  
myArray[2] has 10 elements.  
myArray[3] has 10 elements.  
myArray[4] has 10 elements.  
myArray[5] has 10 elements.  
myArray[6] has 10 elements.  
myArray[7] has 10 elements.  
myArray[8] has 10 elements.  
myArray[9] has 10 elements.  
myVector[0] has 10 elements.  
myVector[1] has 10 elements.  
myVector[2] has 10 elements.  
myVector[3] has 10 elements.  
myVector[4] has 10 elements.  
myVector[5] has 10 elements.  
myVector[6] has 10 elements.  
myVector[7] has 10 elements.  
myVector[8] has 10 elements.  
myVector[9] has 10 elements.  

Process returned 0 (0x0)   execution time : 0.011 s  
Press any key to continue.  

Basically with vectors: your data, their sizes, and lifespan are managed for you; 基本上是使用向量:为您管理数据,数据的大小和寿命; exception vector of pointers to "new" data. 指向“新”数据的指针的异常向量。 Whereas in a primitive dynamic arrays, you'll have to keep track of your arrays sizes (all of them) and delete your data/arrays when done with. 在原始的动态数组中,您必须跟踪数组大小(所有大小),并在完成后删除数据/数组。

Edit: Minor spelling check. 编辑:较小的拼写检查。

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

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