我想读/反序列化一个文件中的元素列表(然后过滤掉其中的一些)。为此目的使用迭代器是一种有用的方法?使用C++迭代器从文件中读取列表?
我现在的尝试是
#include <boost/iterator/iterator_adaptor.hpp>
class ReadIterator : public boost::iterator_adaptor<ReadIterator, Elem *, boost::single_pass_traversal_tag>
{
public:
explicit ReadIterator(const char *filename) : reader(filename) {}
private:
friend class boost::iterator_core_access;
void increment() {
this->base_reference() = reader.readNext();
}
Reader reader;
};
这并不正确释放内存(例如,readNew返回一个指向新ELEM),什么是做到这一点的正确方法?另外,如何实际使用这样一个迭代器,如何确定结束?还是有更好的方法比使用迭代器?
你能给我们采样的输入? – wilhelmtell 2011-04-19 04:32:43