1

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

向量大小从 MC_sim 后的 6 变为我将其传递给 NM_sim 后的 13。任何关于如何修复它的想法或意见都非常感谢!谢谢!

4

2 回答 2

1

您比较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;
于 2013-09-16T17:10:03.740 回答
0

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

于 2013-09-16T17:11:51.413 回答