c++有迭代器但没有容器的类
我正在尝试实现一个类,该类允许我迭代 STL 样式的对象,而无需将它们显式存储在容器中。
一个简化的例子是,类中的
实际上没有 Paragraphs 的容器,而是有一个
另外,我将其称为
而不是
的原因是因为我可能想要一个迭代器一些不同的类型。例如,我可以计算每个段落中的字符数,并有一个
。
我想我的问题是这样的:当没有容器时,用迭代器来思考是否合适?
谢谢
I'm trying to implement a class that would allow me to iterate over objects STL-style without explicitly storing them in a container.
A simplified example of this would be, say, a <Paragraph>::iterator
in a class that doesn't actually have a container of Paragraphs, but instead has a <string> text
variable. It's easy to create a member function that actually goes through text line by line and assemble paragraphs but it seems silly to me to store all this text again in some container just so that I could inherit from it's iterators.
Also, the reason I called it a <Paragraph>::iterator
as opposed to a <string>::iterator
is because I may want to have an iterator of some different type. For example, I could count the number of chars in each of the paragraphs and have an <int>::iterator
.
I guess my question is this: is it appropriate to think in terms of iterators when there's no container?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不仅是合适的,而且是一种优越的思维方式:类应该具有精益接口 - 也就是说,它们应该只公开他们需要公开的内容,仅此而已。如何在内部处理段落(是否将它们存储在容器中,如果是,存储在哪个容器中)是实现细节,不属于类的接口。
在任何情况下,该类都应该只公开段落的迭代器范围。一旦您在接口级别上摆脱了容器,就可能没有理由在类中拥有一个容器,正如您自己所注意到的那样。
It’s not only appropriate, it’s the superior way of thinking: classes should have lean interfaces – that is, they should only expose what they need to expose, nothing more. How you handle paragraphs internally (whether you store them in a container, and if so, in which container) is an implementation detail and doesn’t belong in the class’ interface.
The class should in any case only expose an iterator range of the paragraphs. And once you’ve thus got rid of the container on the interface level there might not be a reason to have one inside the class, as you’ve noticed yourself.
是的,迭代器概念完全独立于容器的概念。
例如,标准库有一组流迭代器。任何可以迭代的东西都应该用 C++ 中的迭代器来表示,无论是否有底层的内存容器。
Yes, the iterator concept is completely independent of the concept of a container.
The standard library has a set of stream iterators, for example. Anything that can be iterated over should be represented by iterators in C++, whether or not there is an underlying in-memory container.
Boost.Range 就是围绕这个概念而设计的。
http://www.boost.org/doc/libs/release /libs/range/index.html
Boost.Range is geared around this concept.
http://www.boost.org/doc/libs/release/libs/range/index.html
在我看来,您正在将
Paragraph
对象视为段落的容器。但是Paragraph
不会是Paragraph
容器的一部分吗?每个Paragraph
都是单词容器(或字符串,具体取决于您的需要)?那么您可以在Paragraph
对象中使用std::string
迭代器,并使用 std 容器来保存Paragraph
吗?您仍然可以创建一个自定义迭代器来迭代您想要的任何内容,无论您想要什么,这个问题的答案 很好地概述了如何构建迭代器类。
Sounds to me like you are treating a
Paragraph
object as a container of paragrahs. But wouldn't aParagraph
be part of a container ofParagraph
s? and eachParagraph
be a container of words (or a string, depending on your needs)? so you could use thestd::string
iterator in theParagraph
object, and a std container to hold theParagraph
?You can still create a custom iterator to iterate over what ever you want, however you want, the answer to this question gives a good overview of how to structure your iterator class.