簡體   English   中英

如何使用 c++ 中的指針將未知大小的多維 arrays 傳遞到 function 中?

[英]How do I pass multi-dimensional arrays of unknown size into a function using pointers in c++?

就像問題說的那樣,我正在嘗試將多維 arrays 傳遞給 function 以將其打印到工程項目的文件中。 輸入數據的格式不能更改,所以請不要建議我將其輸入為不同的數據類型。

這個特殊的 function 預期一個二維數組(盡管我在這個數組之后還有其他三個維度),在運行時之前對數組的大小一無所知。 我知道我必須使用指針分別指向數組的每一行,但我不知道將它傳遞給 function 的語法是什么。 在下面的代碼中,有問題的數組是“塊”。 主要的 function 只是我為使其工作而制作的一個小測試示例:

#include<fstream>
using namespace std;

void of_write_blocks(string filename, string block_type[], int **block,
            int grid[][3], string grade_type[], int grade[][3], int n_blocks, int m[])
{
    ofstream file_out(filename.c_str(),ios::app);
    file_out<<"\nblocks\n(\n";

    for(int i=0;i<n_blocks;++i) {
            file_out<<"   "<<block_type[i]<<" ( ";
                    for(int j=0;j<m[i];++j)
                            file_out<<block[i][j]<<" ";
            file_out<<") ( ";
            file_out<<grid[i][0]<<' '<<grid[i][1]<<' '<<grid[i][2]<<" ) ";
            file_out<<grade_type[i]<<" ( ";
            file_out<<grade[i][0]<<' '<<grade[i][1]<<' '<<grade[i][2]<<" )\n";
    }
    file_out<<");\n";
}


//testing example:
int main()
{       
    int block[6][9];
    for(int i=0; i<6;++i) 
            for(int j=0; i<9;++j)
                    block[i][j] = i*j;

    int grid[6][3];
    for(int i=0; i<6;++i)
            for(int j=0; i<3;++j)
                    block[i][j] = i*j;


    int grade[6][3];
    for(int i=0; i<6;++i) 
            for(int j=0; i<3;++j)
                    block[i][j] = i*j;

    string grade_type[6] = {"simpleGrading"};
    string block_type[6] = {"hex"};
    int m[6] = {8};
    int n_blocks = 6;

    of_write_blocks("name",block_type,block,grid,grade_type,grade,n_blocks,m);
}       

任何幫助表示贊賞!

你不能。 多維 arrays 是語法糖,直接編譯到對數組進行操作的代碼中,即單個 memory 塊。 尺寸不會作為參數或任何類似的東西作為數組的一部分傳遞到 function 中,就像在 Java 或 C# 中所做的那樣。

如果您需要 function 中數組的維度,您只需要接受指向數組第一個元素的指針和維度,然后進行乘法和加法以自己獲得正確的索引。

或者,使用類似std::vector<std::vector<block>>的東西,它將維度作為 object 的一部分而不是內置數組傳遞。

如果您安裝了 Boost,請查看Boost Multi-Array

為清楚起見,我從您的示例中刪除了所有不相關的代碼。

#include <iostream>
#include <fstream>

using namespace std;

void of_write_blocks(int **block, int bh, int bw){

  for(int i = 0; i < bh; ++i)
    for(int j = 0; j < bw; ++j)
      cout << block[i][j] << " ";
  cout << endl;
}

int main(){       

  int bh, bw;
  cin >> bh >> bw;

  int** block;
  block = new int*[bh];
  for(int k = 0; k < bh; k++)
    block[k] = new int[bw];

  // initialize the array
  for(int i = 0; i < bh; i++)
    for(int j = 0; j < bw; j++)
      block[i][j] = (i*bw) + j;

  of_write_blocks( block, bh, bw);
}       

我們主要是創建一個二維數組並對其進行初始化。 然后我們將它傳遞給 of_write_block,它會打印數組。 那是你想做的嗎?

為什么不能使用數組的引用。 請參見下面的示例:

char c[10];
int i[10][20];
double d[10][20][30];

像這樣編寫一個包裝器function:

template<typename T, int SIZE>
void Array (T (&a)[SIZE])
{}
template<typename T, int SIZE1, int SIZE2>
void Array (T (&a)[SIZE1][SIZE2])
{}
template<typename T, int SIZE1, int SIZE2, int SIZE3>
void Array (T (&a)[SIZE1][SIZE2][SIZE3])
{}

這只是一個示例,用於演示無需任何復制即可優雅地接收數組並避免混淆指針的語法。 此外,如果您知道您將僅用於int ,那么只需刪除類型名並明確提及int IE

template<int SIZE>
void Array (int (&a)[SIZE]); // explicitly mention int

暫無
暫無

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

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