如何在 C++ 中將 2D 陣列傳遞給函式

Jinku Hu 2023年1月30日 2020年10月15日
  1. 使用 [] 記號傳遞 2D 陣列作為函式引數
  2. 使用&符號傳遞 2D 陣列作為函式引數
如何在 C++ 中將 2D 陣列傳遞給函式

本文將介紹如何在 C++ 中傳遞一個 2D 陣列作為函式引數。

使用 [] 記號傳遞 2D 陣列作為函式引數

為了演示這個方法,我們定義一個固定長度的二維陣列,命名為 c_array,為了將它的每個元素乘以 2,我們將傳遞一個 MultiplyArrayByTwo 函式作為引數。請注意,這個函式是一個 void 型別,直接對 c_array 物件進行操作。這樣,我們將直接從 main 例程中訪問 2D 陣列的乘法版本。

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

void MultiplyArrayByTwo(int arr[][size], int len)
{
    for (int i = 0; i < len; ++i) {
        for (int j = 0; j < len; ++j) {
            arr[i][j] *= 2;
        }
    }
}

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 (int i = 0; i < size; ++i) {
        cout << " [ ";
        for (int j = 0; j < size; ++j) {
            cout << setw(2) << c_array[i][j] << ", ";
        }
        cout << "]" << endl;
    }

    MultiplyArrayByTwo(c_array, size);

    cout << "output array\n";
    for (int i = 0; i < size; ++i) {
        cout << " [ ";
        for (int j = 0; j < size; ++j) {
            cout << setw(2) << c_array[i][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, ]
output array
 [  2,  4,  6,  8, ]
 [ 10, 12, 14, 16, ]
 [ 18, 20, 22, 24, ]
 [ 26, 28, 30, 32, ]

使用&符號傳遞 2D 陣列作為函式引數

另外,你也可以使用&引用符號來傳遞一個 2D 陣列作為引數。但要注意,這兩種方法只與堆疊上宣告的固定長度陣列相容。請注意,我們把 MultiplyArrayByTwo 函式迴圈改成了基於範圍的,這只是出於可讀性的考慮。

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

void MultiplyArrayByTwo(int (&arr)[size][size])
{
    for (auto & i : arr) {
        for (int & j : i)
            j *= 2;
    }
}

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 (int i = 0; i < size; ++i) {
        cout << " [ ";
        for (int j = 0; j < size; ++j) {
            cout << setw(2) << c_array[i][j] << ", ";
        }
        cout << "]" << endl;
    }

    MultiplyArrayByTwo(c_array);

    cout << "output array\n";
    for (int i = 0; i < size; ++i) {
        cout << " [ ";
        for (int j = 0; j < size; ++j) {
            cout << setw(2) << c_array[i][j] << ", ";
        }
        cout << "]" << endl;
    }
    cout << endl;
    return EXIT_SUCCESS;
}
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