如何使用函数初始化矢量数组 [英] How to initialize a vector array using a function
本文介绍了如何使用函数初始化矢量数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我必须使用很多数组,例如vector< vector>< double> > A1,...一个。一个接一个地初始化它们并不是一个好主意,这就是我所做的。我想知道是否有办法使用初始化函数来实现它。附件是我想要使用的,但它不起作用。
谢谢,
Paul
Hello,
I have to use many arrays like vector<vector><double> > A1,...An. It is not really a good idea to initialize them one after one, which is what I did. I was wondering if there is a way using a initializing function to make it. Attached is what I am trying to use, yet it does not work.
Thanks,
Paul
void mtx_initialize( vector<vector< double> > A,int row, int col){
A.resize(row);
for(int r=0;r<row;r++) A[r].resize(col);
return;
}
推荐答案
假设您的代码已构建并运行,则缺少一件。
您需要传递对向量的引用。你正在制作和丢弃副本。
Assuming your code builds and runs, you have one piece missing.
You need to pass a reference to the vector. As is, you're making and discarding a copy.
void mtx_initialize( vector<vector< double> > & A,int row, int col){
A.resize(row);
for(int r=0;r<row;r++) A[r].resize(col);
return;
}
这篇关于如何使用函数初始化矢量数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文