迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

如何在 C++ 中从函数中返回 2D 数组

作者:迹忆客 最近更新:2023/03/15 浏览次数:

本文将介绍如何在 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, ]

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便