Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将具有可变边界的二维数组传递给函数_C++_Arrays_Function - Fatal编程技术网

C++ 将具有可变边界的二维数组传递给函数

C++ 将具有可变边界的二维数组传递给函数,c++,arrays,function,C++,Arrays,Function,我有一个数组谁的边界由另一个变量(不是常数)定义: 现在我有了一个使用该数组的函数,但是我不知道如何将数组传递给该函数。我该怎么做 为了更清楚地说明这一点,我该如何实现这一点(我考虑过使用类,但变量max是由用户输入定义的,因此我不能使数组成为类的成员,因为max必须是常量) 提前感谢。 如果数组大小在函数中保持不变,则可以考虑使用指针加数组大小的第二个参数。 void function(int *array, const int size) { } 如果您想改变函数中的大小,您可能会考虑ST

我有一个
数组
谁的边界由另一个变量(不是常数)定义:

现在我有了一个使用该
数组的函数,但是我不知道如何将数组传递给该函数。我该怎么做

为了更清楚地说明这一点,我该如何实现这一点(我考虑过使用类,但变量
max
是由用户输入定义的,因此我不能使数组成为类的成员,因为
max
必须是常量)


提前感谢。

如果数组大小在函数中保持不变,则可以考虑使用指针加数组大小的第二个参数。

void function(int *array, const int size)
{
}

如果您想改变函数中的大小,您可能会考虑STD实现:像STD::Vector .< /P> <代码>

#include <iostream>    
using namespace std;

int main() {
    int** Matrix;                     //A pointer to pointers to an int.
    int rows,columns;
    cout << "Enter number of rows: ";
    cin >> rows;
    cout << "Enter number of columns: ";
    cin >> columns;
    Matrix = new int*[rows];         //Matrix is now a pointer to an array of 'rows' pointers.

    for(int i=0; i<rows; i++) {
        Matrix[i] = new int[columns];    //the i place in the array is initialized
        for(int j = 0;j<columns;j++) {   //the [i][j] element is defined
                cout<<"Enter element in row "<<(i+1)<<" and column "<<(j+1)<<": ";
            cin>>Matrix[i][j];
        }
    }
    cout << "The matrix you have input is:\n";

    for(int i=0; i < rows; i++) {
        for(int j=0; j < columns; j++)
            cout << Matrix[i][j] << "\t";   //tab between each element
        cout << "\n";               //new row
    }
    for(int i=0; i<rows; i++)                
        delete[] Matrix[i];         //free up the memory used
}
使用名称空间std; int main(){ int**Matrix;//指向指向int的指针的指针。 int行、列; cout>行; cout>列; Matrix=new int*[rows];//Matrix现在是指向“rows”指针数组的指针。
对于(int i=0;i <代码> max < /Cord>)必须是常量。C++使用STL类型,如<代码> STD::vector < /C> >。在C++中,您不能定义可变长度数组。是否有一种方法可以在VC++中传递VLA?答案是肯定的。它称为“使用A<代码> STD::vector < /代码>,并通过引用传递它。听一下
vector
的建议,将2d数组传递给函数会很快变得混乱。因此,如果不使用常量,就无法传递数组?我知道可以使用向量,但我正在尝试使用数组。这将有助于按用户定义的大小创建数组。然后获取数组/矩阵,并将其传递给数组void
函数(int*array)
谢谢!我想我现在知道了。
void function(int *array, const int size)
{
}
#include <iostream>    
using namespace std;

int main() {
    int** Matrix;                     //A pointer to pointers to an int.
    int rows,columns;
    cout << "Enter number of rows: ";
    cin >> rows;
    cout << "Enter number of columns: ";
    cin >> columns;
    Matrix = new int*[rows];         //Matrix is now a pointer to an array of 'rows' pointers.

    for(int i=0; i<rows; i++) {
        Matrix[i] = new int[columns];    //the i place in the array is initialized
        for(int j = 0;j<columns;j++) {   //the [i][j] element is defined
                cout<<"Enter element in row "<<(i+1)<<" and column "<<(j+1)<<": ";
            cin>>Matrix[i][j];
        }
    }
    cout << "The matrix you have input is:\n";

    for(int i=0; i < rows; i++) {
        for(int j=0; j < columns; j++)
            cout << Matrix[i][j] << "\t";   //tab between each element
        cout << "\n";               //new row
    }
    for(int i=0; i<rows; i++)                
        delete[] Matrix[i];         //free up the memory used
}