[英]Passing a 2D array of unknown size into a function in C++
我正在嘗試將2D數組輸入到函數中。 我不知道該數組的行數或列數,它是通過CImg加載到c ++中的。 這就是我所擁有的:
// Main function:
int main()
{
int rows, columns;
float summation;
CImg<unsigned char> prettypicture("prettypicture.pgm");
rows = prettypicture.height();
columns = prettypicture.width();
summation = SUM(prettypicture[][], rows, columns);
}
// Summation function:
float SUM(int **picture, int rows, int column)
{
... // there is some code here but I don think this is important.
}
我想將數組傳遞給求和函數,並且我知道我應該以某種方式使用指針,但是我不確定如何做到這一點。 任何幫助將非常感激。
謝謝
(很抱歉成為菜鳥)
嘗試這個:
summation = SUM(prettypicture.data(), rows, columns);
並使您的SUM函數如下所示:
float SUM(char* picture, int rows, int column) ...
您需要傳遞data
(如果您想要指向數據的指針),因為這就是CImg提供的。 它是指向字符的指針,因為這就是您所擁有的CImg。 這是一個char*
,而不是char**
,因為這就是數據所提供的。
您沒有向我們展示SUM函數的內部,所以我想知道您是否可以很好地傳遞CImg而不是僅傳遞其數據,並調用具有位置的成員函數atXY
。 看不見就很難說。
有關CImg的data
和其他成員函數的更多信息,請參見http://cimg.eu/reference/structcimg__library_1_1CImg.html 。
為什么不通過它作為參考?
summation = SUM(prettypicture);
float SUM(const CImg<unsinged char>& picture) {
// ...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.