将向量转移到函数 [英] Transfer vector to function
本文介绍了将向量转移到函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个在其上使用vector的c ++代码.我想从主函数的另一个函数中调用此向量.
Hi,
I have one c++ code that used vector on that. I want call this vectors from another function from main function.
int main ()
{
vector<double> ld_12;
vector<double> ld_24;
double d=f0(ld_12,ld_24);
.
.
.
return;
}
double f0(double *a0,double*a1){
.
.
.
}
我想知道如何在标头f0中调用两个向量:
双重f0(double * a0,double * a1)
此致
I want know how call two vectors in header f0:
double f0(double *a0,double*a1)
Regards,
推荐答案
int main ()
{
vector<double> ld_12;
vector<double> ld_24;
double d = f0(&ld_12, &ld_24);
// ...
return;
}
double f0(vector<double>* a0, vector<double>* a1)
{
double dValue = 0.0;
dValue = a0->at(0);
// ...
return dValue;
}
更多代码
More code
int main()
{
vector<double> ld_12;
vector<double> ld_24;
/// ... i assume there is some code to fill the 2 vectors
double d = f0(ld_12, ld_24);
return 0;
}
// The & means the vectors are modifyable.
double f0 ( vector<double>& a0, vector<double>& a1 )
{
double value = 0.0;
// get the size of the vectors.
size_t a0_size = a0.size();
size_t a1_size = s1.size();
// do something.
return value;
}
</double></double></double></double>
2个选项:
1-如果可能,请重构代码以使f0接收向量而不是数组.
2-调用f0
,例如:
2 options :
1- If possible, refactor the code to have f0 receive vectors instead of arrays.
2- callf0
like :
f0( &ld_12[0], &ld_24[0] );
之所以行之有效,是因为该标准告诉我们,向量是在连续内存中分配的(例如arrary).
另外,不要忘记将2个数组的大小传递到您的f0
函数.
This works because the standard tells us that the vector is allocated in continuous memory (like an arrary).
Also, don''t forget to pass the size of the 2 arrays somewhere to your f0
function.
这篇关于将向量转移到函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文