通用迭代器
我正在尝试找到一种访问一组容器的通用方法。 除了另一个自定义列表之外,我还有一个标准向量和列表。
自定义列表定义了一个迭代器;
class Iterator: public std::iterator<std::forward_iterator_tag, T> {
// ...
}
Iterator begin() {
return (Iterator(root));
}
Iterator end() {
return (Iterator(NULL));
}
适当的运算符重载。
理想情况下,我想这样做;
class Foo {
public:
Foo() {
std::list<int> x;
std::vector<int> y;
custom_list<int> z;
iter = x.begin(); // OR
iter = y.begin(); // OR
iter = z.begin();
// ...
};
private:
std::iterator<int> iter;
};
但显然这些都是不同类型的迭代器。 不过,我可以假设所有容器都是同一类型。
有没有一种优雅的方法来解决这个问题?
I am trying to find a generic way of accessing a set of containers. I have a standard vector and list in addition to another custom list.
The custom list defines an iterator;
class Iterator: public std::iterator<std::forward_iterator_tag, T> {
// ...
}
Iterator begin() {
return (Iterator(root));
}
Iterator end() {
return (Iterator(NULL));
}
with the appropriate operators overloaded.
Ideally, I would like to do this;
class Foo {
public:
Foo() {
std::list<int> x;
std::vector<int> y;
custom_list<int> z;
iter = x.begin(); // OR
iter = y.begin(); // OR
iter = z.begin();
// ...
};
private:
std::iterator<int> iter;
};
But obviously these are all iterators of different types. I can assume all the containers are of the same type however.
Is there an elegant way to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个对你的要求要小心的例子。 您看到的 any_iterator 类适用于无限的迭代器类型集。 你只有三个,这是你事先知道的。 当然,您将来可能需要添加第四种类型,但是如果这需要 O(1) 额外的代码行怎么办?
可能包含的类型的封闭集的一大优点是 sizeof() 具有上限,这意味着您可以避免堆及其带来的间接性。 基本上,将它们全部放入 boost::variant 中并调用 apply_visitor。
A case of being careful what you ask for. The any_iterator classes you see work on an unbounded set of iterator types. You only have three, which you know up front. Sure, you might need to add a fourth type in the future, but so what if that takes O(1) extra lines of code ?
The big advantage of a closed set of possible contained types is that you have an upper bound on sizeof(), which means you can avoid the heap and the indirection it brings. Basically, stuff them all in a boost::variant and call apply_visitor.
以下是您可能感兴趣的一些文章
为 STL 迭代器提供基类
C++ 迭代器的类型擦除
any_iterator 类参考
Here are some articles you might find of interest
Giving STL Iterators a Base Class
Type Erasure for C++ Iterators
any_iterator Class Reference
迟到总比不到好……
最新一期的 C-Vu 出现并猜测其中包含什么:没错,迭代器完全可以实现您想要的功能。
不幸的是,您需要成为 ACCU 的会员才能查看该杂志(该文章引用了 2000 年的 Overload 文章,David链接到)。 但一年只需花费微薄的费用,您就可以获得一本不错的杂志、会议和用户团体。 当您成为会员后,您可以查看过刊的 PDF 版本,所以您还在等什么?
Better late than never...
The latest issue of C-Vu turned up and guess what was in it: That's right, iterators that do exactly what you wanted.
Unfortunately you need to become a member of the ACCU to view the magazine (the article references the Overload article from 2000 that David links to). But for a measly price a year you get a nice magazine to read, conferences and user groups. When you become a member you can view PDF's of the back issues so what are you waiting for?