我正在努力将FindFirstFile/ FindNextFileloop中的丑陋内容(尽管我的问题适用于其他类似的API,例如RegEnumKeyEx或RegEnumValue等)在迭代器内部以类似于标准模板库的方式工作istream_iterator.
我这里有两个问题.第一个是大多数"foreach"风格循环的终止条件.STL样式迭代器通常operator!=在for的退出条件内使用,即
std::vector<int> test;
for(std::vector<int>::iterator it = test.begin(); it != test.end(); it++) {
//Do stuff
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我不确定如何operator!=使用这样的目录枚举来实现,因为我不知道枚举何时完成,直到我实际完成它.我现在有一个混合解决方案,它现在枚举整个目录,每个迭代器只是跟踪引用计数向量,但这似乎是一个可以做得更好的方法的kludge.
我遇到的第二个问题是FindXFile API返回了多个数据.因此,operator*根据迭代器语义的要求,没有明显的重载方法.当我重载该项时,是否返回文件名?尺寸?修改日期?我怎样才能传达这样一个迭代器必须在后来以一种思维方式引用的多个数据?我试过扯掉C#风格的MoveNext设计,但我担心这里没有遵循标准的习语.
class SomeIterator {
public:
bool next(); //Advances the iterator and returns true if successful, false if the iterator is at the end.
std::wstring fileName() const;
//other kinds of data....
};
Run Code Online (Sandbox Code Playgroud)
编辑:调用者看起来像:
SomeIterator x = ??; //Construct somehow
while(x.next()) {
//Do stuff
}
Run Code Online (Sandbox Code Playgroud)
谢谢! …