簡體   English   中英

向量 <double> 在C ++代碼中加倍錯誤

[英]vector <double> to double error in c++ code

我正在嘗試返回在加權隨機分布上返回的向量中的值(即,如果向量具有1,2,3,它將給出1/6的機會是1,2 / 6的機會是2,並且a 3/6的機會是3)。 我收到的編譯時間錯誤

Protein.cpp:809:54: error: cannot convert ‘__gnu_cxx::__normal_iterator<double*, std::vector<double> >’ to ‘double’ in assignment
Protein.cpp:814:24: error: could not convert ‘((Protein*)this)->Protein::fitness1.std::vector<_Tp, _Alloc>::operator[]<double, std::allocator<double> >((std::vector<double, std::allocator<double> >::size_type)index)’ from ‘double’ to ‘std::vector<double>’

當我編譯代碼時,嘗試修復它的運氣很少。 如果有人可以看一下功能並為我提供幫助,我將不勝感激。 再次感謝您的寶貴時間!

功能

vector<double> Protein::Init_Seq_Recursive(State& s, int d)
{
    vector<float> sequence;
    double index;
    float initial;
    int i =0;
    for (i; i < k; i++)
    {
        s[d] = i;
        if (d == (L - 1))
        {
            int ind = Index(s);


            Ef[ind] = GetEf(s);
            Eb1[ind] = GetEb1(s);
            Eb2[ind] = GetEb2(s);
            double zf = exp(beta_f*Ef[ind]);
            double zb1 = exp(beta_b*Eb1[ind]);
            double zb2 = exp(beta_b*Eb2[ind]);


            fitness1[ind] = (1. + f_ub*zb1 + f_ub*f_uf*zf*zb1)/( 1. + zb1 + zf*zb1 );
            fitness2[ind] = (1. + f_ub*zb2 + f_ub*f_uf*zf*zb2)/( 1. + zb2 + zf*zb2 );



        }       
        else
            Init_Fitness_Recursive(s, d + 1);
    }
    if(i==k-1){
        vector<float> weights;
        float running_total = 0;


        for(int y = 0; y<pow(k,L); y++){
            running_total = running_total+fitness1[y];
            weights.push_back(running_total);


        }
        double rnd = (static_cast <double> (rand()) / static_cast <double> (RAND_MAX))*running_total;
        for(int y = 0; y<fitness1.size(); y++){
            if(rnd<weights[y]){
            index = find(fitness1.begin(), fitness1.end(), rnd);
            }       
        }

    }
    return(fitness1[index]);

}

std::find不返回索引。 它返回指向找到的元素的迭代器。 要獲取index ,您需要計算begin迭代器與找到的迭代器之間的距離。 像這樣:

vector<double>::iterator found = find(fitness1.begin(), fitness1.end(), rnd);
index = distance(fitness1.begin(), found);

相當懷疑您是否會使用double來索引容器。

另外,您的return(fitness1[index]); 正在嘗試返回單個double ,但是您的返回類型為vector<double>

index = find(fitness1.begin(), fitness1.end(), rnd);

find的返回值是一個迭代器。 您需要取消引用以獲得double精度(即index的類型):

index = *find(fitness1.begin(), fitness1.end(), rnd);
//      ^

其次,你試圖返回一個double從該聲明為返回一個函數vector<double>

return( fitness1[index] );
//      ^^^^^^^^^^^^^^^

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM