我有一个包含此数据的文件:
3
0 0.5
1 0.23
100 -0.5
2
1 0.01
6 -0.55
我有两个私有成员x和y变量的私有结构和列表:
struct FTerm {
int m_delay;
double m_weight;
};
std::list<FTerm> m_xterms;
std::list<FTerm> m_yterms;
结果应包含
for 3 m_xterms:
m_delay contains 0, 1, 100
m_weight contains 0.5, 0.23, -0.5
for 2 m_yterms:
m_delay contains 1, 6
m_weight contains 0.01, -0.55
到目前为止,我有这个:
ifstream filt(dlg.GetPathName());
if (filt.fail())
{
AfxMessageBox(L"Failed to open filter transfer equation file");
return;
}
int numXTerms;
string line;
while (getline(filt, line))
{
istringstream iss(line);
FTerm term;
while (iss >> numXTerms)
{
filt >> numXTerms >> term.m_delay >> term.m_weight;
m_xterms.resize(numXTerms);
m_xterms.push_back(term);
}
}
从调试开始,这就是我得到的,所以我不确定它是否真的有效:
我还需要弄清楚如何在换行后获取Y值,有没有办法检查换行符并写入另一个while循环?
答案 0 :(得分:0)
你有一些不太正确的事情:
这样的事情应该有效:
std::vector<FTerm> m_xterms;
std::vector<FTerm> m_yterms;
int numFTerms;
std::string line;
filt >> numFTerms;
m_xterms.resize(numFTerms);
for (int i = 0; i < numFTerms; i++)
{
FTerm term;
filt >> term.m_delay >> term.m_weight;
m_xterms[i] = term;
}
//ignore empty line
filt.ignore(1);
filt >> numFTerms;
m_yterms.resize(numFTerms);
for (int i = 0; i < numFTerms; i++)
{
FTerm term;
filt >> term.m_delay >> term.m_weight;
m_yterms[i] = term;
}