首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C++中的动态二维数组作为参数

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

Stack Overflow用户
提问于 2018-09-21 07:39:45
回答 1查看 91关注 0票数 0

对于使用动态编程的问题,我有一个二维双精度数组。我想做的事情如下(f是在变量"size“上操作的任何函数,并返回一个int):

代码语言:javascript
运行
复制
void myFunction(int size){
    double myArr[size][f(size)];
    helperFunction(size, myArr);
}

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

但是,这段代码显然是java和c++的混搭。我如何在c++中实现这一点呢?

EN

回答 1

Stack Overflow用户

发布于 2018-09-21 14:41:07

强烈建议使用向量作为动态数组的替代方法。向量在java中名声不佳,但在c++中却是大势所趋。演示使用“新”分配和向量来实现这一点的“一种”c++方法。警告:与java不同,c++中的原始数组要求您跟踪数组大小;以及所有内部数组。

代码语言:javascript
运行
复制
#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;
}

打印结果以显示大小:

代码语言:javascript
运行
复制
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.  

基本上是向量:你的数据,它们的大小和生命周期都是为你管理的;指向“新”数据的指针的异常向量。而在原始的动态数组中,您必须跟踪您的数组大小(所有数组),并在完成后删除数据/数组。

编辑:次要拼写检查。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52434688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档