【发布时间】:2020-02-02 13:12:01
【问题描述】:
作为运行时分析的一部分,我有一个小游戏,它在计算每个帧后都会在此列表中添加一个新元素:
typedef std::list<std::pair<float, float>> PairList;
PairList Frames; //in pair: index 0 = elapsed time, index 1 = frames
txt 文件稍后用于绘制图形。 我决定使用列表,因为在玩游戏时我不需要处理列表中保存的数据,而且我认为列表是仅添加或删除项目时最快的容器。作为下一步,我想将帧写入外部 txt 文件。
void WriteStats(PairList &pairList)
{
// open a file in write mode.
std::ofstream outfile;
outfile.open("afile.dat");
PairList::iterator itBegin = pairList.begin();
PairList::iterator itEnd = pairList.end();
for (auto it = itBegin; it != itEnd; ++it)
{
outfile << *it.first << "\t" << *it.second;
}
outfile.close();
}
对于普通列表,指向“it”的指针应该返回项目对吗?
除了visual studio说pair<float, float>*没有一个叫first的成员
当通过我的迭代器访问不起作用时,我想怎么做?是不是因为我传入了对列表的引用?
【问题讨论】:
-
(1,2) (3,4) (5,6) 的输出将是
1\t23\t45\t6... 可能难以解析为输入。