传递给函数后,向量的大小会发生变化

时间:2013-09-16 17:02:21

标签: c++ vector pass-by-reference

我正在处理一个向量,它是我的代码的最终输出。我发现的是,即使我没有向它添加任何元素,我将它传递给函数之前和之后的矢量大小也是不同的。我通过引用传递矢量。有问题的函数是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 &param)
{
    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 ################

在将传递给NM_sim之后,向量大小从MC_sim之后的6变为13。任何有关如何解决它的想法或意见表示赞赏!谢谢!

2 个答案:

答案 0 :(得分:1)

您的param_pertparam的比较尺寸。这两个不是相同的载体。

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 :(得分:0)

第一步:消除除来回传递之外的所有代码。你还有问题吗?如果没有,那么问题出在您的功能代码中。如果问题仍然存在,那么至少你知道这是导致你的问题的传递。