当维度大小不清楚时将多维数组传递给函数 [英] passing multidimensional arrays to function when the dimension size is not clear

查看:31
本文介绍了当维度大小不清楚时将多维数组传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级中有一个函数应该得到一个多维数组.问题是这些维度的值是在类中计算的,使用另一个函数并存储在一些公共变量中.所以类中的所有其他函数都可以读取存储在这些变量中的值.函数的定义是这样的:

I have a function in my class which should get a multi-dimensional array. the problem is that the value of these dimensions is calculated in the class, using another function and is stored in some public variables. so all the other functions within the class can read the value stored in these vars. the definition of function is like this:

double calcSS(double tfpairexp[][max_ctrl_no][max_rep_no])

其中 max_ctrl_nomax_rep_no 是那些 public 变量.我这样调用函数:

where max_ctrl_no and max_rep_no are those public variables. and I call the function like this:

calcSS(tfpairexp);

但是编译器给出了函数定义错误.如果我放一些常数而不是那些变量,就没有错误.但我不知道程序开始时的维度.我该怎么办?

but the compiler gives error for the definition of the function. and if I put some constant numbers instead of those vars, there is no error. But I don't know the dimension when the program begins. what should I do?

更新

void tripleofgenes(int begind,int endind) {             
            for (int tf1=0; tf1<tfact; ++tf1)
            {   
                    for (int tf2=tf1+1; tf2<tfact; ++tf2)
                    { 
                        double tfpairexp[cchips][max_ctrl_no][max_rep_no];
                        double w,d;
                        int ctrl_no;int c, ctr;
                        for (int j = 0; j < cchips ; j += c)
                        {
                            c = chips[j].crepls;
                            int ctrl_no=0;
                            for (int *p = chips[j].ctrl ; p && (ctr=*p)>=0; ++p,ctrl_no++) {
                                for (int k = 0; k < c; ++k)
                                {
                                    double tf1exp=chips[k+j].vals[tf1].val-((chips[k+j].useextra) ? chips[ctr].extra[tf1] : chips[ctr].mean[tf1].val);
                                    double tf2exp=chips[k+j].vals[tf2].val-((chips[k+j].useextra) ? chips[ctr].extra[tf2] : chips[ctr].mean[tf2].val);
                                    tfpairexp[j][ctrl_no][k]=interactionFunc(tf1exp,tf2exp,1);
                                }
                            }
                        }
                        for (int tg=begind; tg<=endind; ++tg) 
                            if (tf1!=tg){

                        calcSS(tfpairexp);

                        }


                    }
            }
        fclose(f);
    }

    double calcSS(double ***tfpairexp)
    {
    for (int row = 0; row <= 1 ; row++)
                for (int col = 0; col <= 1 ; col++) {
                    int cond=0;
                    for (int j = 0; j < cchips; j += c) {
                        c = chips[j].crepls; // the number of replicants
                        int ctrl_no=0;
                        for (int *p = chips[j].ctrl; p && (ctr=*p)>=0; ++p,ctrl_no++) {
    a = tfpairexp[j][ctrl_no][k];}
    }
    }
    }
    }

推荐答案

您在此处使用的数组类型(C 样式)并非真正设计用于动态大小,尽管您可以使用 new 以这种方式分配它们.函数调用的一种解决方案是使用 double *** 和三个 size_t 参数将维度传递给函数,但这仍然需要您在调用者的尺寸与您需要的一样大.否则你会破坏你的进程内存并可能崩溃.

The kind of arrays in you are using here (C-style) are not really designed to be used with dynamic sizes, though you can use new to allocate them that way. One solution for function calling is do use a double *** and three size_t parameters for dimensions to pass into a function, but that still requires you to declare the array in the caller with dimensions as large as you will ever need. or you will corrupt your process memory and likely crash.

更好的解决方案是使用 vector,它从一开始就设计用于处理具有运行时确定大小的数组.您可以声明一个多维向量并直接使用它,也可以使用一维向量并在访问时进行一些数学运算,将三元组坐标转换为向量的索引.

A better solution is to use vector which is designed from the beginning to deal with array with runtime-determined sizes. You can either declare a multi-D vector and use it directly, or go for a single-dimensional vector and do a bit of math at access time to convert a triple of coordinates into an index into the vector.

无论哪种方式,您都需要矢量标头,以下是我建议放置该设置的方式:

Either way, you need the vector header, here is how I recommend placing that setup:

#include <vector> // in your includes

using std::vector; // at global scope immediately after all #includes

一些代码示例.我将根据另一个答案使用 cchips 作为第三维的大小.多维解决方案如下所示:

Some code samples. I'll use cchips for the size of the third dimension based on another answer. The multidimensional solution looks something like this:

// at global scope
typedef vector< vector<double> > vec2double;
typedef vector< vector<vec2double> > vec3double;

// in the caller of your calcSS after you know how big to make the array {
    vec3double tfairexp(cchips, vec2double(max_ctrl_no, vector<double>(max_rep_no)));
    // now fill up tfairexp
    calcSS(tfairexp);
// }

// important: pass a reference here, or you will copy the whole array at call time!
void calcSS(vec3double &tfairexp) {
    // you can use cchips etc here but don't have to:
    for (size_t i = 0; i < tfairexp.size(); i++) {
        for (size_t j = 0; j < tfairexp[0].size(); j++) {
            for (size_t k = 0; k < tfairexp[0][0].size(); k++) {
                // access tfairexp[i][j][k] here
            }
        }
    }
}

该解决方案实际上会进行动态数组的1+j+j*k 分配.您可以改为编写一个包装类,该类将拥有"向量并进行维度转换,并让您当前正在编写的类使用它.我正在做出一些风格上的牺牲,以继续使用您的维度大小的公共类成员变量"方法.然而,这个更接近地模拟了编译器如何转换 C 风格的多维数组,并且每次运行只分配一次内存:

That solution will actually do 1+j+j*k allocations of dynamic arrays. You could instead write a wrapper class that would "own" the vector and do the dimension-translation stuff, and have the class you're currently writing use that. I'm making some stylistic sacrifices to continue to use your "public class member variables for dimension sizes" approach. However, this one more closely models how the compiler translates C-style multidimensional arrays, and only does one memory allocation per run:

// as a member of your class -- returns a live reference that can be assigned to!
double &vec3_get(vector<double> &array, const size_t i, const size_t j, const size_t k) {
    return array[i*max_ctrl_no*max_rep_no + j*max_rep_no +k];
}

// in caller of calcSS {
    vector<double> tfairexp(cchips*max_ctrl_no*max_rep_no);
    // fill in by doing vec3_get(tfairexp, i, j, k) = val;
    calcSS(tfairexp)
// }

// again, pass a reference to the vector!
void calcSS(vector<double> &tfairexp) {
    for (size_t i = 0; i < cchips; i++) {
        for (size_t j = 0; j < max_ctrl_no; j++) {
            for (size_t k = 0; k < max_rep_no; k++) {
                // access vec3_get(tfairexp, i, j, k) here
            }
        }
    }
}

这篇关于当维度大小不清楚时将多维数组传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆