简体   繁体   English

我怎样才能拥有一个带有二维数组作为参数的 function,而该数组有一个我想更改的参数/维度?

[英]how can I have a function with a 2D array as an argument whereas the array has a parameter/dimension that I want to change?

(I'm a student and this is my first time posting so go easy on me.) I want to create a function that takes a 2D array as an argument and in that array, I'd like to have a variable that I want to modify later in the code. (我是一名学生,这是我第一次发帖,所以 go 对我来说很容易。)我想创建一个 function ,它将二维数组作为参数,在该数组中,我想要一个我想要的变量稍后在代码中修改。 This is the closest thing to an example of what I want:这是最接近我想要的例子的东西:

int size;  //the variable I want to change later
void function(int[][size]);

int main(){
    cin >> size;
    int array[size][size];  //the array I'm using with the variable as a parameter
    function(array)
}

void function(int array[][size]){
    //Do thing....
}

The code above does give me an error (array bound is not an integer constant) so if I make the variable a constant it will compile as seen here:上面的代码确实给了我一个错误(数组绑定不是 integer 常量)所以如果我将变量设为常量,它将编译如下所示:

const int size = 10;
void function(int[][size]);

int main(){
    int array[size][size];
    function(array)
}

void function(int array[][size]){
    //Do thing....
}

This does compile like I said, but now I can't modify the variable and need to declare its value in the code beforehand.这确实像我说的那样编译,但现在我不能修改变量,需要事先在代码中声明它的值。 I assume that the variable needs to be global so that I can use it in the function, and with that said, I can't get pointers to work either most likely because it's a global variable and not a local one.我假设该变量需要是全局变量,以便我可以在 function 中使用它,话虽如此,我也很可能无法获得工作指针,因为它是全局变量而不是本地变量。 Here's an example of something I tried, but got an error (invalid conversion from 'const int*' to 'int*'):这是我尝试过的示例,但出现错误(从 'const int*' 到 'int*' 的无效转换):

const int size = 10;
void function(int[][size]);

int main(){
    int *other = &size;
    *other = 5;
}

Any help would be appreciated, thanks.任何帮助将不胜感激,谢谢。

Plain ol' arrays aren't resizeable in C++.普通的 ol' arrays 在 C++ 中不可调整大小。 Even more frustrating, their size has to be a constant - you can't make the size a variable that gets set at runtime.更令人沮丧的是,它们的大小必须是一个常数——你不能让大小成为在运行时设置的变量。 Ever more frustrating, the size you put in an array that's a function parameter is a constraint , and it's not even enforced.更令人沮丧的是,您放入一个 function 参数的数组中的大小是一个约束,甚至没有强制执行。 It's just decor.这只是装饰。

As it was hinted in the comments, std::vector<TYPE> is the go-to "resizeable array" in C++.正如评论中所暗示的, std::vector<TYPE>是 C++ 中的首选“可调整大小的数组”。 You can create a vector like this:你可以像这样创建一个向量:

#include <vector>
int main() {
    std::vector<int> my_int_array;
}

And you can resize it like this:你可以像这样调整它的大小:

int new_size = 42;
my_int_array.resize(new_size);

And you can pass it to a function by reference (see the & ) so that changes to myint_array inside the function affect it outside the function.您可以通过引用将其传递给 function(请参阅& ),以便对 function 内部的 myint_array 的更改会在 ZC1C425268E68385D1AB5074C17A94F1 外部影响它。

void my_awesome_function(std::vector<int>& int_array);

my_awesome_function(my_int_array);

So let's say you have a 2D matrix, implemented as a vector of vectors:所以假设你有一个二维矩阵,实现为向量的向量:

std::vector<std::vector<int>> matrix = { { 1,2,3 }, { 4,5,6 } }

If you want to change the number of columns, you have to resize each row array:如果要更改列数,则必须调整每个行数组的大小:

int new_column_count = 10;
for (auto& row : matrix) {
    row.resize(new_column_count );
}

You can pass around matrix by reference (eg std::vector<std::vector<int>>& ) and resize it when you need to.您可以通过引用传递matrix (例如std::vector<std::vector<int>>& )并在需要时调整它的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM