使用 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 的指针),正确的方法是什么?另外,如何实际使用这样的迭代器,如何确定结束?或者有比使用迭代器更好的方法吗?
I am trying to read/deserialize a list of elements from a file (and then filter out some of them). It is a useful approach to use an iterator for this purpose?
My current try is
#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;
};
This does not properly deallocate memory (e.g., readNew returns a pointer to a new Elem), what is the right way to do this? Also, how would one actually use such an iterator, how can the end be determined? Or is there a better approach than using an iterator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
执行此操作的简单方法是使用 std::istream_iterator
标准算法从输入文件复制对象(属于 YourObjectClass 类型),并将它们放入向量数据中,如果过滤函子返回 true。
唯一的条件是:
简单的工作示例:
Exmpale:
The easy way to do this is to use the std::istream_iterator
The standard algorithm copies objects (of type YourObjectClass) from the input
file
and places them into the vectordata
if the filter functor returns true.The only conditions are:
Simple Working Example:
Exmpale:
您是否可以构造调用,以便它返回一个引用计数的智能指针,而不是返回指向元素的原始指针,当指针的引用计数达到时,该指针将自动释放其资源变为零?要么,要么你必须找到一种方法来取回指针,以便你可以在通过下一次调用
readNext()< 分配更多内存之前对其调用
delete
/code> 当再次调用increment()
时。至于“结束”,在这种情况下您可以做的是在您的 Reader 类中进行一些测试,检测您是否已到达文件末尾或其他结束场景。如果有,则返回 false,否则返回 true。例如:
现在你可以在某种类型的循环中调用increment(),你就会知道何时到达文件结尾或其他结尾,因为该函数将返回 false 。
Rather than
readNext()
returning a raw pointer to an element, can you construct the call so that it returns a reference-counted smart-pointer that will automatically release it's resources when the reference-count to the pointer goes to zero? Either that, or you're going to have to find a way to fetch the pointer back so you can calldelete
on it before you allocate more memory via the next call toreadNext()
whenincrement()
is called again.As for the "end", what you could do in this case is have some test in your
Reader
class that detects if you've reached the end of the file or some other ending scenario. If you have, then return false, otherwise return true. For example:So now you could call
increment()
in some type of loop, and you'll know when you've hit the end-of-file or some other ending because the function will return false.使用迭代器来达到这个目的是可以的。不过,您没有任何迹象表明现有的
istream_iterator
无法满足您的目的。至少在大多数情况下,您只需为单个元素编写一个operator>>
,然后使用std::istream_iterator
从文件中读取这些元素的列表。Using an iterator for the purpose is fine. You haven't given any indication that the existing
istream_iterator
won't work for your purpose though. At least in most cases, you can just write anoperator>>
for a single element, and usestd::istream_iterator
to read a list of those elements from the file.