且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

传递二维数组以发挥作用

更新时间:2023-11-12 07:53:27

有两种方法可以实现您的目标。

第一种方式使用动态分配的C风格数组。除非您***支持现有代码,否则您不应该在C ++中执行此操作。但在这种情况下,***考虑重构,也许。

第二种方式使用 std :: vector 。这个类用内存封装了低级操作。它会让你摆脱许多潜在的错误。

为了向你展示 std :: vector 的优点,我写了两个程式。他们输入二维数组的大小(换句话说,它被称为矩阵)并创建它。



使用C风格的数组创建矩阵需要大量的代码和操作与指针。 为简单起见,以下代码不处理异常。在实际代码中,您应该这样做以避免内存泄漏,但代码会变得更难。

  #include< iostream> 
#include< cstddef>

使用namespace std;

void function(int ** matrix,size_t nrows,size_t ncols)
{

}

int main()
{
size_t nrows;
size_t ncols;

cin>> nrows>> NCOLS;

//为矩阵分配内存
int **矩阵=新的int * [nrows];
for(size_t i = 0; i matrix [i] = new int [ncols];

函数(矩阵,nrows,ncols);

//释放内存
for(size_t i = 0; i delete [] matrix [i];
delete [] matrix;





$ b

所以在C ++中使用 std会更容易::矢量。由于 std :: vector 是一个类,它具有构造函数和析构函数封装来分配和释放内存。

  #include< iostream> 
#include< vector>

使用namespace std;

void function(vector< vector< int>>& matrix)
{

}

int main()
{
size_t nrows;
size_t ncols;

cin>> nrows>> NCOLS;

//创建矩阵
vector< vector< int>>矩阵(NROWS); (size_t i = 0; i< nrows; ++ i)
matrix [i] = vector< int>(ncols)的
;

//你甚至不需要传递矩阵
函数(矩阵)的大小;

//自动调用matrix的析构函数释放内存
}


I am new to programming.

User will be enter the size of 2D array and then I want to pass this array to function. How can I do this?

There are two ways to accomplish your goal.

The first way is using dynamically allocated C-style arrays. You shouldn't do this in C++ unless you are forced to support existing code. But in this case it is better to consider refactoring, maybe.

The second way is using std::vector. This class encapsulates low-level manipulations with memory. It will free you from many potential bugs.

To show you advantages of std::vector I have written two programs. They input sizes of 2D array (in another way, it is called a matrix) and create it.

Creating matrix using C-style arrays requests a lot of code and manipulations with pointers. For the sake of simplicity the following code doesn't handle exceptions. In real code you should do it to avoid memory leaks, but the code would become even harder.

#include <iostream>
#include <cstddef>

using namespace std;

void function(int** matrix, size_t nrows, size_t ncols)
{

}

int main()
{
    size_t nrows;
    size_t ncols;

    cin >> nrows >> ncols;

    // allocate memory for matrix
    int** matrix = new int*[nrows];
    for (size_t i = 0; i < nrows; ++i)
        matrix[i] = new int[ncols];

    function(matrix, nrows, ncols);

    // release memory
    for (size_t i = 0; i < nrows; ++i)
        delete[] matrix[i];
    delete[] matrix;
}

So in C++ it would be very easier using std::vector. Since std::vector is a class it has constructor and destructor encapsulating allocating and releasing memory.

#include <iostream>
#include <vector>

using namespace std;

void function(vector<vector<int>>& matrix)
{

}

int main()
{
    size_t nrows;
    size_t ncols;

    cin >> nrows >> ncols;

    // create matrix
    vector<vector<int>> matrix(nrows);
    for (size_t i = 0; i < nrows; ++i)
        matrix[i] = vector<int>(ncols);

    // you don't even need to pass sizes of matrix
    function(matrix);

    // automatically called destructor of matrix releases memory
}