簡體   English   中英

如何返回一維尺寸未知的二維數組?

[英]How to return a 2d array where one dimension is of unknown size?

這與這個問題有關 我有一個函數void doConfig(mappings, int& numOfMappings) ,我不確定如何聲明映射。 它是一個二維數組,其元素為char。 第一維在運行時確定,並將在函數主體中計算。 第二維始終為2。此代碼是什么? 我想象它是char** mappings或類似的東西。 同樣在C ++中,數組總是通過引用傳遞嗎? 因此&即使我打算在函數返回時使用該值,也不需要使用&

編輯:基本上我想返回此 char (*mapping)[2] = new char[numOfMappings][2];

按照2to1mux的建議,我仍然無法正常工作。 數組似乎獲得了正確的值,但是當doConfig()函數返回時出現了問題。

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
}

好吧,我現在可以正常工作了,但主要是因為閑逛。 人們能否在此解釋有關如何知道何時使用*&解決方案?

由於您標記的是C ++而不是C,所以我想您可能需要一個適當的解決方案。

template<typename T>
using vectorOf2D = std::vector<std::array<T, 2>>;

vectorOf2D<char> getMappings() {
    return /* whatever you do to fill those */;
    // (most probably) using NRVO to ellide the copy
}

而且,如果您擔心訪問可能會很復雜:

auto mappings = getMappings();

functionTakingAMapping(mappings[i]);
char element = mappings[0][1];

(緊隨我對鏈接問題的回答。)

直接的(至今相當復雜的)語法是

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;
}

我會先后回答您的問題:

  1. 正確,您無需在此處使用&

  2. 從技術上講,“按引用”一詞不適用於傳遞數組,但是對您的問題的簡單回答是,您永遠不會將數組的副本傳遞給函數。 對array類型的參數所做的任何更改都將應用於原始數組,而不是副本。

  3. 我建議傳遞一個雙指針:

     void doConfig(char **mappings, int& numOfMappings) 

    您將能夠像訪問二維數組一樣完全訪問映射成員。 例:

     mappings[2][3] = 'b'; 

編輯:這是根據您的澄清提出的新建議

void doConfig(char** mappings, int& numOfMappings){

    /*Compute numOfMappings -- 
      this integer is passed by-reference, so it can be used outside function
      to figure out the size allocated within the function*/

    mappings = new char*[numOfMappings];
    for(int i=0; i < numOfMappings; i++){
        mappings[i] = new char[2];
    }
    /*Do whatever you need to do with mappings*/

    /*Return nothing because function is void -- since mappings is passed as
      pointer, changes are maintained after function ends*/

}

您可以返回一個指向2D數組的指針。

例如,

char **ptr;


return ptr;

傳遞數組的地址時,如果要傳遞2D數組起始位置的地址而不是特定元素的地址,則不必使用&運算符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM