2010-12-02 70 views
1

問題是迭代器沒有迭代循環。我不知道爲什麼。 #includes在頭上,就像我的習慣一樣。此迭代器不迭代,最新錯誤?

#include "neutronFileReader.h" 

using namespace std ; 

neutronFileReader::neutronFileReader() 
{ 
} 

list<vector<float> > neutronFileReader::spectrum(char* filename) 
{ 
    ifstream fin(filename) ; 
    string binhi, binlo ; 
    list<vector<float> > neutronSpectrum ; 
    list<vector<float> >::iterator nS ; 
    vector<float> EnergyProbability ; 

    while(!fin.eof()) 
    { 
     getline(fin, binlo, ' ') ;      //get the binlo string 
     cout << "binlo: "<<binlo << endl ; 
     getline(fin, binhi, ' ') ;      //get the binhi string 
     cout<<"binhi: "<<binhi<<endl ; 
     EnergyProbability.push_back(atof(binhi.c_str())+(atof(binhi.c_str()) - atof(binlo.c_str()))/2) ; //store middle of bin as emission Energy 
     getline(fin, binlo) ;       //try not to waste memory space 
     cout<<"prob: "<<binlo<<endl ; 
     EnergyProbability.push_back(atof(binlo.c_str())) ; //store emnission probability 
     neutronSpectrum.push_back(EnergyProbability) ; //put the vector in the list 
     //cout<<neutronSpectrum<<endl ; 
    } 


    for(nS = neutronSpectrum.begin() ; nS != neutronSpectrum.end() ; nS++) //go through the neutron spectrum 
    { 
     EnergyProbability = (*nS) ; 
     cout << "binval: " << EnergyProbability[0] << " " << "binProb: " << EnergyProbability[1] << endl ; 
     cout << "binval: " << (*nS)[0] << ", binprob: " << (*nS)[1] << ", memadd: " << &nS << endl ;   // print energy & prob to screen 
    } 

    return neutronSpectrum ; 
} 

反正這裏一些幫助將不勝感激,已經向它變成一個while循環,是的,這是所有錯誤的測試,但它的代碼非常重要的一點。歡呼傢伙,一直在學習。

+0

您是否嘗試首先打印列表大小?只是爲了確保你真的有一個列表來遍歷:) – 2010-12-02 08:59:22

+3

最新的問題,它不會編譯?它會崩潰嗎?爲了幫助你,請多描述一下情況。 – Reno 2010-12-02 08:59:35

+2

是什麼讓你覺得它不是迭代? – 2010-12-02 09:00:07

回答

1

您不清除輸入循環之間的能量可能性。 (* ns)[0]因此會從第一個輸入中看到冗餘存儲的值,忽略實際存在於[2]中的新值,然後[4]等。在讀取更多值之前,只需添加EnergyProbability.clear()即可。

1

你確定你正在填充neutronSpectrum數組,所以它不是空的?請務必加入這個到最後:

if (neutronSpectrum.empty()) 
    cerr << "error: empty neutronSpectrum" << endl; 

也許有你的輸入文件有問題(它是空的或不可讀的),所以你最終擺在首位添加沒什麼neutronSpectrum。只是爲了確保,請將cout聲明添加到您的while循環中。此外,它是值得的while循環後檢查fin.error()

if (fin.error()) 
    cerr << "error: error reading input file: " filename << endl; 
0

如果沒有獲取在最後neutronSpectrumfor -loop印刷可能是空的。

0

好的,檢查了上述所有內容,其中一個問題是我沒有清除Energy循環向量的每次循環(不是它是空的),而是創建了一個更大更大的向量,每次回到列表的後面,所以指向列表元素(A向量)的前兩個元素被輸出,並且每次都是相同的(顯然,我是一個傻瓜)。所以,現在我知道這個問題實際上是迭代器沒有意識到它已經返回列表的末尾,並且當它被doees時它會拋出一個空指針,我想。代碼在這裏:

#include "neutronFileReader.h" 

using namespace std ; 

neutronFileReader::neutronFileReader() 
{ 
} 

list<vector<float> > neutronFileReader::spectrum(char* filename) 
{ 
ifstream fin(filename) ; 
string binhi, binlo ; 
list<vector<float> > neutronSpectrum ; 
list<vector<float> >::iterator nS ; 
vector<float> EnergyProbability ; 

while(!fin.eof()) 
{ 
    EnergyProbability.clear() ; 
    getline(fin, binlo, ' ') ;      //get the binlo string 
    cout << "binlo: "<<binlo << endl ; 
    getline(fin, binhi, ' ') ;      //get the binhi string 
    cout<<"binhi: "<<binhi<<endl ; 
    EnergyProbability.push_back(atof(binhi.c_str())+(atof(binhi.c_str()) - atof(binlo.c_str()))/2) ; //store middle of bin as emission Energy 
    getline(fin, binlo) ;       //try not to waste memory space 
    cout<<"prob: "<<binlo<<endl ; 
    EnergyProbability.push_back(atof(binlo.c_str())) ; //store emnission probability 
    cout << EnergyProbability[0] << ":" << EnergyProbability[1] << endl ; 
    neutronSpectrum.push_back(EnergyProbability) ; //put the vector in the list 
} 

for(nS = neutronSpectrum.begin() ; nS != neutronSpectrum.end() ; nS++) //go through the neutron spectrum 
{ 
    EnergyProbability = (*nS) ; 
    cout << &neutronSpectrum.begin() << " : " << &nS << " : " << &neutronSpectrum.end() << endl ;   // print energy & prob to screen 
} 


return neutronSpectrum ; 
} 

輸出爲:

0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 

然後代碼::塊必須意識到這是一個無限循環,其中沒有什麼改變並退出代碼。

歡呼傢伙