如何返回一个二维数组,其中一个维度是未知大小的? [英] How to return a 2d array where one dimension is of unknown size?

查看:390
本文介绍了如何返回一个二维数组,其中一个维度是未知大小的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关系到<一个href=\"http://stackoverflow.com/questions/16456286/creating-a-2d-array-one-dimension-not-known-at-compile-time\">this问题。我有一个函数无效doConfig(映射,INT和放大器; numOfMappings),我不知道如何申报的映射。它是一个二维阵列,其元素字符。第一维是在运行时确定的,并且将在函数体来计算。第二个维度是总是将2.什么是code这个?我想像它是的char **映射或类似的东西。此外,在C ++中数组总是通过引用传递吗?所以,我并不需要使用&放大器; 即使我打算使用的值当函数返回

This is related to this question. I have a function void doConfig(mappings, int& numOfMappings) and I'm not sure how to declare mappings. It is a two dimensional array whose elements are chars. The first dimension is determined at run time, and will be computed in the body of the function. The second dimension is always going to be 2. What is the code for this? I'd imagine it to be char** mappings or something like that. Also in C++ arrays are always passed by reference right? So I don't need to use & even though I intend to use the value when the function returns?

编辑:基本上,我想回到这个 字符(*映射)[2] =新的char [numOfMappings] [2];

根据2to1mux的建议,我仍然不能得到它的工作。该阵列似乎得到正确的价值观,但事情错了,当 doConfig()函数返回。

as per 2to1mux's suggestion I still cannot get it to work. The array appears to getting the right values but something is going wrong when the doConfig() function returns.

int main()
{
    int numOfMappings = 0;
    char **mappings;
    doConfig(mappings, numOfMappings);
    cout << "This is mappings" << mappings << endl;//this address is different than the one given in doConfig(), is that wrong?
    cout << "this is numOfMappings: " << numOfMappings << endl;
    cout << mappings[0][0] << "->" << mappings[0][1] << endl;//program crashes here
    //code removed
    return EXIT_SUCCESS;
}

void doConfig(char **mappings, int& numOfMappings)
{
    //code removed, numOfMappings calculated
    for(int j = 0; j < numOfMappings; j++)
    {
        getline(settingsFile, setting);
        mappings[j] = new char[2];
        mappings[j][0] = setting.at(0);
        mappings[j][1] = setting.at(2);
    }
    for(int j = 0; j < numOfMappings; j++)
        cout << mappings[j][0] << "->" << mappings[j][1] << endl;//everything is as expected so array created ok
    cout << "This is mappings" << mappings << endl;//different address than the one give in main
}

确定我得到了现在,但主要是从工作围绕黄克竞。难道人请解释有解决方案,他们怎么知道何时使用 * &放大器;

OK I got it working now but mainly from haking around. Could people please explain there solutions as to how they known when to use * and &?

推荐答案

(在我回答这个链接的问题采取后续行动。)

(Following up on my answer to the linked question.)

这样做的直接(还颇为曲折)语法将

The direct (yet rather convoluted) syntax for this would be

char (*create_mappings(size_t n))[2]
{
  // Allocate an char[n][2] array
  char (*mappings)[2] = new char[n][2];

  // Initailize `mappings[i][j]` in any way you want...

  return mappings;
}

但是,一个更好的想法是让它通过的typedef

typedef char Char2[2];

Char2 *create_mappings(size_t n)
{
  // Allocate an char[n][2] array
  Char2 *mappings = new Char2[n];

  // Initailize `mappings[i][j]` in any way you want...

  return mappings;
}

这篇关于如何返回一个二维数组,其中一个维度是未知大小的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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