且构网

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

如何将二维向量传递给 C++ 中的函数?

更新时间:2022-04-07 00:19:14

自从你的函数声明:

void printMatrix(vector< vector<int> > *matrix)

指定一个指针,它本质上是通过引用传递的.但是,在 C++ 中,***避免使用指针并直接传递引用:

specifies a pointer, it is essentially passed by reference. However, in C++, it's better to avoid pointers and pass a reference directly:

void printMatrix(vector< vector<int> > &matrix)

printMatrix(matrix1); // Function call

这看起来像一个普通的函数调用,但它是通过引用传递的,如函数声明中所示.这可以避免不必要的指针取消引用.

This looks like a normal function call, but it is passed by reference as indicated in the function declaration. This saves you from unnecessary pointer dereferences.