我在哪里可以获得 C++ 的示例代码为我自己的容器创建迭代器?
我一直在寻找为我自己的容器创建迭代器的示例代码,但我还没有真正找到一个好的例子。我知道以前有人问过这个问题(创建我自己的迭代器),但没有看到任何满意的答案并举例说明。
我正在寻找简单的示例代码来开始如何设计我自己的迭代器。
谢谢
I have been searching for sample code creating iterator for my own container, but I haven't really found a good example. I know this been asked before (Creating my own Iterators) but didn't see any satisfactory answer with examples.
I am looking for simple sample code to start how to design my own iterator.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这里您可以找到有关创建自定义迭代器的良好介绍。另请查看 Boost.Iterator图书馆。
Here you could find good intro for creating custom iterators. Also take a look on the Boost.Iterator Library.
我发现 Matthew Wilson 的“扩展 STL”非常主题。包含大量注意事项,以及大量实用的编程技巧。我认为这个人真的知道他在做什么。 (也为此创建了库)
I found Matthew Wilson's 'extended STL' very educative on the subject. Contains lots of do's and don'ts, plus tons of practical programming tips. I think this guy really knows what he's doing. (created libraries for that, too)
Nicolai Josuttis 在他的书中有一个用户定义迭代器的示例:C++ 标准库、教程和参考。
这是在线示例:
http://www.josuttis.com/libbook/iter/ assoiter.hpp
http://www.josuttis.com/libbook/iter/assoiter.cpp
Nicolai Josuttis has an example of a user defined iterator in his book: C++ Standard Library, a tutorial and a reference.
Here is the example online:
http://www.josuttis.com/libbook/iter/assoiter.hpp
http://www.josuttis.com/libbook/iter/assoiter.cpp
看一下这篇文章,它描述了如何为您的类实现自定义虚拟迭代器:文章
它有一个显着的优点 - 您可以创建一个抽象基本迭代器类,并为您自己的容器(也许还有一些 STL 容器)继承它并使用一些自定义迭代器。因此,您将能够动态地使用迭代器 - 您的函数将使用指向抽象迭代器类的指针,而其他代码将能够选择应使用哪些容器。
Take a look on this article which describes how to implement a custom virtual iterator for your classes: article
It has one significant advantage - you can create an abstract base iterator class and inherit it with a few custom iterators for your own containers and maybe for some STL containers. So you will be able to use iterators dynamically - your functions will use a pointer to the abstract iterator class while other code will be able to choose what containers should be used.