[英]Size of vector changes after passing to a function
我正在研究向量,这是我代码的最终输出。 我发现的是,即使不向其中添加任何元素,向量大小在传递给函数之前和之后也有所不同。 我正在通过参考传递矢量。 有问题的函数是NM_sim,我无法调试为什么会发生这种情况。 感谢您的时间和帮助! 我正在跟踪将向量传递给函数NM_sim之前和之后的大小。 调用NM_sim后,矢量的大小会更改。 这是我的代码的一部分:
state_type被描述为std :: vector
random_select(gene_ind, n_ka_temp, n_kd_temp, kavec_pert, kdvec_pert, kaval_pert, kdval_pert);
state_type param_pert;
param_pert.push_back(param[0]);
param_pert.push_back(param[1]);
param_pert.push_back(param[2]);
param_pert.insert(param_pert.end(),kaval_pert.begin(),kaval_pert.end());
param_pert.insert(param_pert.end(),kdval_pert.begin(),kdval_pert.end());
transform(param_pert.begin(),param_pert.end(),param_pert.begin(),powof10());
cout << "########## Value of param size is: " << param.size() << " ################" << endl;
MC_sim ( x_d, t_d, mean_xd, fex_nm, jex_nm, gene_ind, n_ka_temp, n_kd_temp, error_pert, param_pert);
for (int i = 0; i < param.size(); i++)cout << "########## Value of param from MC is: " << param[i] << " ################" << endl;
cout << "########## Value of param size is: " << param.size() << " ################" << endl;
cout << "The optimized value of error from MC calculation is: " << error_pert << endl;
NM_sim( x_d, t_d, mean_xd, fex_nm, jex_nm, gene_ind, n_ka_temp, n_kd_temp, error_pert, param_pert);
cout << "The optimized value of error from NM calculation is: " << error_pert << endl;
在NM_sim内部:
void NM_sim( const state_type &x_d, const state_type &t_d, const state_type &mean_xd, myFex_single &fex_nm, myJex_single &jex_nm, const int &gene_ind, const int nka, const int nkd, double &error_ode, state_type ¶m)
{
const int param_size = 3 + nka + nkd;
cout << "########## Value of error from MC is: " << error_ode << " ################" << endl;
cout << "########## Value of param size is: " << param.size() << " ################" << endl;
for (int i = 0; i < param.size(); i++)cout << "########## Value of param from MC is: " << param[i] << " ################" << endl;
....
}
我得到的输出是:
########## Value of param from MC is: 0.789519 ################
########## Value of param from MC is: -0.47315 ################
########## Value of param from MC is: -0.693194 ################
########## Value of param from MC is: 0.368322 ################
########## Value of param from MC is: 0.298118 ################
########## Value of param from MC is: 0.883191 ################
########## Value of param size is: 6 ################
The optimized value of error from MC calculation is: 0.000329494
########## Value of error from MC is: 0.000329494 ################
########## Value of param size is: 13 ################
########## Value of param from MC is: 0.789519 ################
########## Value of param from MC is: -0.47315 ################
########## Value of param from MC is: -0.693194 ################
########## Value of param from MC is: 0.368322 ################
########## Value of param from MC is: 0.298118 ################
########## Value of param from MC is: 0 ################
########## Value of param from MC is: 0 ################
########## Value of param from MC is: 0.883191 ################
########## Value of param from MC is: 0 ################
########## Value of param from MC is: 0 ################
########## Value of param from MC is: 0 ################
########## Value of param from MC is: 0 ################
########## Value of param from MC is: 0 ################
向量大小从MC_sim之后的6变为我传递给NM_sim之后的13。 任何有关如何解决它的想法或意见,不胜感激! 谢谢!
您比较的param_pert
和param
大小。 这两个不是相同的向量。
cout << "..." << param.size() << "..." << endl;
MC_sim ( x_d, t_d, mean_xd, fex_nm, jex_nm, gene_ind, n_ka_temp, n_kd_temp, error_pert, param_pert)
尝试:
cout << "########## Value of param size is: " << param_pert.size() << " ################" << endl;
步骤1:消除所有代码,除非来回传递。 你还有问题吗? 如果不是,则问题出在您的功能代码中。 如果问题仍然存在,那么至少您知道是造成问题的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.