c++有迭代器但没有容器的类

发布于 2024-11-15 10:38:54 字数 497 浏览 1 评论 0原文

我正在尝试实现一个类,该类允许我迭代 STL 样式的对象,而无需将它们显式存储在容器中。

一个简化的例子是,类中的 ::iterator 实际上没有 Paragraphs 的容器,而是有一个 。文本变量。创建一个实际上逐行遍历文本并组装段落的成员函数很容易,但在我看来,将所有这些文本再次存储在某个容器中以便我可以从它的迭代器继承,这似乎很愚蠢。

另外,我将其称为 ::iterator 而不是 ::iterator 的原因是因为我可能想要一个迭代器一些不同的类型。例如,我可以计算每个段落中的字符数,并有一个 ::iterator

我想我的问题是这样的:当没有容器时,用迭代器来思考是否合适?

谢谢

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷爱 2024-11-22 10:38:54

在没有容器的情况下,用迭代器来思考是否合适?

这不仅是合适的,而且是一种优越的思维方式:类应该具有精益接口 - 也就是说,它们应该只公开他们需要公开的内容,仅此而已。如何在内部处理段落(是否将它们存储在容器中,如果是,存储在哪个容器中)是实现细节,不属于类的接口。

在任何情况下,该类都应该只公开段落的迭代器范围。一旦您在接口级别上摆脱了容器,就可能没有理由在类中拥有一个容器,正如您自己所注意到的那样。

is it appropriate to think in terms of iterators when there's no container?

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.

笑忘罢 2024-11-22 10:38:54

是的,迭代器概念完全独立于容器的概念。

例如,标准库有一组流迭代器。任何可以迭代的东西都应该用 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.

吻泪 2024-11-22 10:38:54

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

野生奥特曼 2024-11-22 10:38:54

在我看来,您正在将 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 a Paragraph be part of a container of Paragraphs? and each Paragraph be a container of words (or a string, depending on your needs)? so you could use the std::string iterator in the Paragraph object, and a std container to hold the Paragraph?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文