#include <iostream>
using namespace std;
void create(int** map);
int main() {
int** map;
create(map);
cout << endl << "printing map in main" << endl;
for (int i = 0; i < x; i ) {
for (int j = 0; j < y; j ) {
map[i][j] = 1;
cout << map[i][j] << " ";
}
cout << endl;
}
return 0;
}
void create(int** map) {
int x = 8, y = 8;
map = new int* [x];
for (int i = 0; i < x; i ) {
map[i] = new int[y];
}
for (int i = 0; i < x; i ) {
for (int j = 0; j < y; j ) {
map[i][j] = 1;
cout << map[i][j] << " ";
}
cout << endl;
}
}
我一直在嘗試從指向 main 函式中的動態陣列的 2D 指標中讀取元素,但我似乎無法讓它作業,我嘗試使用 double for 回圈和( (map i ) j) 但無濟于事
uj5u.com熱心網友回復:
您的代碼進行了一些重構:
#include <iostream>
int** create2DArray( const std::size_t rowCount, const std::size_t colCount );
void delete2DArray( int** const array2D, const std::size_t rowCount );
void print2DArray( int** const array2D, const std::size_t rowCount, const std::size_t colCount );
int main( )
{
constexpr std::size_t ROW_COUNT { 8 };
constexpr std::size_t COL_COUNT { 8 };
int** map { create2DArray( ROW_COUNT, COL_COUNT ) };
std::cout << '\n' << "printing map in main" << '\n';
print2DArray( map, ROW_COUNT, COL_COUNT );
// after work is done with array, delete it
delete2DArray( map, ROW_COUNT );
map = nullptr; // to prevent it from becoming a dangling pointer
return 0;
}
int** create2DArray( const std::size_t rowCount, const std::size_t colCount )
{
int** const array2D = new int* [rowCount];
for ( std::size_t row = 0; row < rowCount; row )
{
array2D[row] = new int[colCount];
}
for ( std::size_t row = 0; row < rowCount; row )
{
for ( std::size_t col = 0; col < colCount; col )
{
array2D[row][col] = 1;
}
}
return array2D;
}
void delete2DArray( int** const array2D, const std::size_t rowCount )
{
for ( std::size_t row = 0; row < rowCount; row )
{
delete[] array2D[row];
}
delete[] array2D;
}
void print2DArray( int** const array2D, const std::size_t rowCount, const std::size_t colCount )
{
for ( std::size_t row = 0; row < rowCount; row )
{
for ( std::size_t col = 0; col < colCount; col )
{
std::cout << array2D[row][col] << " ";
}
std::cout << '\n';
}
}
輸出:
printing map in main
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
注意#1:在constexpr變數宣告中使用時,該變數在編譯時而不是運行時計算,并成為常量運算式。因此,將具有常量值的變數(例如,像5或3.7這樣的立即數)宣告為constexpr.
查看constexpr (C )以獲取更多詳細資訊。
注意#2: std::size_t是無符號整數型別。在我的編譯器上,它相當于unsigned long long int(BTW 的大小為 8 個位元組)。
這是來自這個std::size_t:
typedef /*implementation-defined*/ size_t;
uj5u.com熱心網友回復:
您需要將引數宣告為具有參考型別。例如
void create(int ** &map);
在這種情況下,mapin 中宣告的指標main將在函式內更改。
否則函式處理它的區域變數(引數)map并且map宣告的指標main將保持不變。
此外,使用像8. 如果可以將陣列的大小傳遞給函式,則該函式將更加靈活和通用。例如
void create( int ** &map, size_t m, size_t n )
{
map = new int * [m];
for ( size_t i = 0; i < m; i )
{
map[i] = new int[n];
}
for ( size_t i = 0; i < m; i )
{
for ( size_t j = 0; j < n; j )
{
map[i][j] = 1;
cout << map[i][j] << " ";
}
cout << endl;
}
}
主要你可以寫
size_t m = 8, n = 8;
int **map = nullptr;
create( map, m, n );
cout << endl << "printing map in main" << endl;
for ( size_t i = 0; i < m; i )
{
for ( size_t j = 0; j < n; j )
{
cout << map[i][j] << " ";
}
cout << endl;
}
uj5u.com熱心網友回復:
您應該回傳它,而不是將指標傳遞給 create()。像這樣:
int main(){
int **map = create();
cout << endl << "printing map in main" << endl;
return 0;
}
int ** create() {
int x = 8, y = 8;
int ** map = new int* [x];
for (int i = 0; i < x; i ) {
map[i] = new int[y];
}
for (int i = 0; i < x; i ) {
for (int j = 0; j < y; j ) {
map[i][j] = 1;
cout << map[i][j] << " ";
}
cout << endl;
}
return map;
}
您還應該為分配的矩陣添加洗掉。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/392687.html
