我现在已经对这段代码进行了大量的错误检查,并确保我输出了“正确”的东西来概述问题。迭代器根本不指向列表,而是另一组包含正确数据的地址。
我有两个问题:
1 = 给定 couts 的形式,我是否输出了正确的项目来调查为什么这个循环没有退出;
2 = if(1) 那么是什么产生了这个输出,你有什么建议可以进一步了解我的指针知识吗(我之前已经多次使用这种 for 循环格式,这从未发生过;
/问题
代码 :
#include "neutronFileReader.h"
using namespace std ;
neutronFileReader::neutronFileReader()
{
}
list<vector<float> > neutronFileReader::spectrum(char* filename)
{
ofstream addresses ;
addresses.open("adresses.txt") ;
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
getline(fin, binhi, ' ') ; //get the binhi string
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
EnergyProbability.push_back(atof(binlo.c_str())) ; //store emnission probability
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) ;
addresses << &neutronSpectrum.begin() << " : " << &(*nS) << " : " << &neutronSpectrum.end() << endl ; // print energy & prob to screen
cout << &neutronSpectrum.begin() << " : " << &(*nS) << " : " << &neutronSpectrum.end() << endl ;
}
return neutronSpectrum ;
}
这是输出:
0x28fbc4 : 0x510c38 : 0x28fbc0
0x28fbc4 : 0x510c58 : 0x28fbc0
0x28fbc4 : 0x510c78 : 0x28fbc0
0x28fbc4 : 0x510c98 : 0x28fbc0
0x28fbc4 : 0x510cb8 : 0x28fbc0
0x28fbc4 : 0x510cd8 : 0x28fbc0
0x28fbc4 : 0x510cf8 : 0x28fbc0
0x28fbc4 : 0x510d18 : 0x28fbc0
0x28fbc4 : 0x510d38 : 0x28fbc0
0x28fbc4 : 0x510d58 : 0x28fbc0
0x28fbc4 : 0x510d78 : 0x28fbc0
0x28fbc4 : 0x510d98 : 0x28fbc0
0x28fbc4 : 0x510db8 : 0x28fbc0
0x28fbc4 : 0x510dd8 : 0x28fbc0
0x28fbc4 : 0x510df8 : 0x28fbc0
0x28fbc4 : 0x510e18 : 0x28fbc0
0x28fbc4 : 0x510e38 : 0x28fbc0
0x28fbc4 : 0x510e58 : 0x28fbc0
0x28fbc4 : 0x510e78 : 0x28fbc0
0x28fbc4 : 0x510e98 : 0x28fbc0
0x28fbc4 : 0x510eb8 : 0x28fbc0
非常感谢。