Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+中的动态二维数组+;作为参数_C++_Multidimensional Array - Fatal编程技术网

C++ C+中的动态二维数组+;作为参数

C++ C+中的动态二维数组+;作为参数,c++,multidimensional-array,C++,Multidimensional Array,我有一个使用动态规划求解问题的二维双精度数组。我想做的如下(f是对变量“size”进行操作并返回int的任何函数): 但是,这个代码显然是java和C++的混搭。如何在C++中实现这一点?高度推荐使用向量作为动态数组的替代。向量在java中有坏名声,但是C++中它们是一条路。演示“A”C++实现这一方法,同时使用“新”的分配和向量。警告:与java不同,C++中的原始数组将要求您跟踪数组大小;所有内部数组也是如此 #include <iostream> #include <ve

我有一个使用动态规划求解问题的二维双精度数组。我想做的如下(f是对变量“size”进行操作并返回int的任何函数):


<>但是,这个代码显然是java和C++的混搭。如何在C++中实现这一点?

高度推荐使用向量作为动态数组的替代。向量在java中有坏名声,但是C++中它们是一条路。演示“A”C++实现这一方法,同时使用“新”的分配和向量。警告:与java不同,C++中的原始数组将要求您跟踪数组大小;所有内部数组也是如此

#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;
}
基本上是向量:你的数据,它们的大小和寿命是为你管理的;指向“新”数据的指针的异常向量。而在基本的动态数组中,您必须跟踪数组大小(所有数组),并在处理完后删除数据/数组


编辑:小拼写检查.< /P>可变长度数组无效C++。您的编译器可能支持将它们作为扩展。您希望在此处使用

std::vector
而不是
double[][]
。。。如果需要传递一个子集,则传递整个向量以及4个整数(rowstart、rowend、colstart、colend)或。。。双**myArr,int w,int h)自双myArr[大小][f(大小)];必须动态分配编写一个简单的矩阵类,如下所示:
#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;
}
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.