如何在 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