传递具有可变大小的2D数组 [英] Passing 2D array with variable Size

查看:49
本文介绍了传递具有可变大小的2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将2D数组从一个函数传递给另一个函数.但是,数组的大小不是恒定的.大小由用户确定.

I'm trying to pass a 2D array from a function to another function. However, the size of the array is not constant. The size is determined by the user.

我试图对此进行研究,但是运气不高.大多数代码和说明都是针对数组的恒定大小.

I've tried to research this, but haven't had much luck. Most code and explanations are for a constant size of the array.

在函数A中,我声明了变量,然后对其进行了一点操作,然后必须将其传递给函数B.

In my function A I declare the variable and then I manipulate it a bit, and then it must be passed to Function B.

void A()
{
      int n;
      cout << "What is the size?: ";
      cin >> n;

      int Arr[n-1][n];

      //Arr gets manipulated here

      B(n, Arr);
}

void B(int n, int Arr[][])
{
    //printing out Arr and other things

}

推荐答案

使用 std::vector 如果要动态调整数组大小:

Use std::vector if you want dynamically-sized arrays:

std::vector<std::vector<int>> Arr(n, std::vector<int>(n - 1));
B(Arr);

void B(std::vector<std::vector<int>> const& Arr) { … }

这篇关于传递具有可变大小的2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆