如何在 C++ 中從函式中返回 2D 陣列

Jinku Hu 2023年1月30日 2020年11月24日
  1. 在 C++ 中使用指標符號從函式中返回 2D 陣列
  2. 在 C++ 中使用指向指標的指標從函式中返回 2D 陣列
如何在 C++ 中從函式中返回 2D 陣列

本文將介紹如何在 C++ 中從函式中返回一個 2D 陣列。

在 C++ 中使用指標符號從函式中返回 2D 陣列

對於較大的物件,通過指標返回是首選方法,而不是通過值返回。由於二維陣列可能會變得相當大,所以最好將指標傳遞給矩陣的第一個元素,如下面的程式碼示例所示。需要注意的是,ModifyArr 中的 2D 陣列引數是用 arr[][SIZE] 符號定義的,以便在函式作用域中用括號訪問其元素。

#include <iostream>
#include <vector>
#include <iomanip>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::setw;

constexpr int SIZE = 4;

int *ModifyArr(int arr[][SIZE], int len)
{
    for (int i = 0; i < len; ++i) {
        for (int j = 0; j < len; ++j) {
            arr[i][j] *= 2;
        }
    }
    return reinterpret_cast<int *>(arr);
}

int main(){
    int c_array[SIZE][SIZE] = {{ 1, 2, 3, 4 },
                               { 5, 6, 7, 8 },
                               { 9, 10, 11, 12 },
                               { 13, 14, 15, 16 }};

    cout << "input array\n";
    for (auto & i : c_array) {
        cout << " [ ";
        for (int j : i) {
            cout << setw(2) << j << ", ";
        }
        cout << "]" << endl;
    }
    cout << endl;

    int *ptr = ModifyArr(c_array, SIZE);

    cout << "modified array\n";
    for (int i = 0; i < SIZE; ++i) {
        cout << " [ ";
        for (int j = 0; j < SIZE; ++j) {
            cout << setw(2) << *(ptr + (i * SIZE) + j) << ", ";
        }
        cout << "]" << endl;
    }
    cout << endl;

    return EXIT_SUCCESS;
}

輸出:

input array
 [  1,  2,  3,  4, ]
 [  5,  6,  7,  8, ]
 [  9, 10, 11, 12, ]
 [ 13, 14, 15, 16, ]

modified array
 [  2,  4,  6,  8, ]
 [ 10, 12, 14, 16, ]
 [ 18, 20, 22, 24, ]
 [ 26, 28, 30, 32, ]

在 C++ 中使用指向指標的指標從函式中返回 2D 陣列

作為一種替代方法,可以使用指標到指標的記法來返回函式中的陣列。如果要返回的物件是動態分配的,這種方法比其他方法有優勢。通常,一旦指標在呼叫者範圍內返回,就應該修改元素訪問表示式。請注意,我們將陣列地址轉換為 int*,然後再去引用來獲取值。

#include <iostream>
#include <vector>
#include <iomanip>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::setw;

constexpr int SIZE = 4;

int **ModifyArr2(int *arr, int len)
{
    for (int i = 0; i < len; ++i) {
        for (int j = 0; j < len; ++j)
            *(arr + (i * len) + j) *= 2;
    }
    return reinterpret_cast<int **>(arr);
}

int main(){
    int c_array[SIZE][SIZE] = {{ 1, 2, 3, 4 },
                               { 5, 6, 7, 8 },
                               { 9, 10, 11, 12 },
                               { 13, 14, 15, 16 }};

    cout << "input array\n";
    for (auto & i : c_array) {
        cout << " [ ";
        for (int j : i) {
            cout << setw(2) << j << ", ";
        }
        cout << "]" << endl;
    }
    cout << endl;

    int **ptr2 = ModifyArr2(c_array[0], SIZE);

    cout << "modified array\n";
    for (int i = 0; i < SIZE; ++i) {
        cout << " [ ";
        for (int j = 0; j < SIZE; ++j) {
            cout << setw(2) << *((int*)ptr2 + (i * SIZE) + j) << ", ";
        }
        cout << "]" << endl;
    }
    cout << endl;

    return EXIT_SUCCESS;
}

輸出:

input array
 [  1,  2,  3,  4, ]
 [  5,  6,  7,  8, ]
 [  9, 10, 11, 12, ]
 [ 13, 14, 15, 16, ]

modified array
 [  2,  4,  6,  8, ]
 [ 10, 12, 14, 16, ]
 [ 18, 20, 22, 24, ]
 [ 26, 28, 30, 32, ]
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn

相關文章 - C++ Array