创建我自己的迭代器 [英] Creating my own Iterators

查看:101
本文介绍了创建我自己的迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习C ++,如果这个问题显示缺乏基本的知识,我会原谅我,你看,事实是,我缺乏基本的知识。

I'm trying to learn C++ so forgive me if this question demonstrates a lack of basic knowledge, you see, the fact is, I have a lack of basic knowledge.

我想要一些帮助,如何为我创建的类创建一个迭代器。

I want some help working out how to create an iterator for a class I have created.

我有一个类'Shape'有一个容器的点。
我有一个类'Piece',引用一个Shape并定义一个Shape的位置。
Piece没有一个Shape它只是引用一个Shape。

I have a class 'Shape' which has a container of Points. I have a class 'Piece' which references a Shape and defines a position for the Shape. Piece does not have a Shape it just references a Shape.

我想看起来像Piece是一个容器的点与那些相同

I want it to seem like Piece is a container of Points which are the same as those of the Shape it references but with the offset of the Piece's position added.

我想要能够遍历Piece的Points,就好像Piece是一个容器本身一样。我做了一个小小的阅读,没有找到任何帮助我。我将非常感谢任何指针。

I want to be able to iterate through the Piece's Points just as if Piece was a container itself. I've done a little reading around and haven't found anything which has helped me. I would be very grateful for any pointers.

推荐答案

您应该使用Boost.Iterators。它包含许多模板和概念来为现有迭代器实现新的迭代器和适配器。我已撰写关于这个主题的文章;它在2008年12月的ACCU杂志。它讨论一个(IMO)优雅的解决方案,正是你的问题:使用Boost.Iterators从一个对象暴露成员集合。

You should use Boost.Iterators. It contains a number of templates and concepts to implement new iterators and adapters for existing iterators. I have written an article about this very topic; it's in the December 2008 ACCU magazine. It discusses an (IMO) elegant solution for exactly your problem: exposing member collections from an object, using Boost.Iterators.

如果你只想使用stl, Josuttis book 有一章介绍实现您自己的STL迭代器。

If you want to use the stl only, the Josuttis book has a chapter on implementing your own STL iterators.

这篇关于创建我自己的迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆