C++ 中的 IEnumerable?

发布于 2024-10-15 11:32:50 字数 671 浏览 1 评论 0原文

我想知道是否有一种通用方法可以在 C++ 中表示高级列表类对象,除了使用 STL 迭代器。 (我所说的高级是指一些复杂的对象,例如数据库响应,而不是低级向量)。可能类似于 C# 的 IEnumerable。如果您见过任何使用此的项目,能给我一个参考吗?谢谢。

编辑:澄清为什么我对此感兴趣,而不是问题的一部分。正如评论中所要求的,这里有一些代码解释了我在编译时多态性和运行时多态性之间看到的关键区别。想象一下,您有一个编译时多态类型,这基本上意味着您有两种具有共同概念(而不是接口)的类型。如果你想对这个多态类型应用一个算法,它必须是通用的:

template<class iterator>
void myalgorithm(iterator iter) {...}

但是当你有一个带有接口的运行时多态类型时,比如说IMyIterator,您可以在其上编写“正常”的非泛型算法。

void myalgorithm(IMyIterator* iter) {...}

实现这两个函数的最大区别在于使用接口与泛型类型的语言级别和 IDE 支持不同。另一点是,并不是每个人都像您一样熟悉模板编程。最后,第二种情况允许隐藏实现,而第一种情况仅包含标头。所以请不要告诉我它们之间没有区别,除非你能告诉我我错在哪里。

I'd like to know if there is a common way to represent high-level list-like objects in C++, other than using STL iterators. (by high-level I mean some complex objects, say a database response, as opposed to a low-level vector). Probably something similar to C#'s IEnumerable. If you have seen any projects using this, could you give me a reference? Thanks.

EDIT: Clarification why I'm interested in this, and not a part of the question. As asked in the comments, here is some code explaining the crucial difference that I see between compile-time polymorphism and run-time one. Imagine that you have a compile-time polymorphic type, which basically means that you have two types that have a common concept (not interface). If you want to apply an algorithm to this polymorphic type, it will have to be generic:

template<class iterator>
void myalgorithm(iterator iter) {...}

But when you have a run-time polymorphic type with an interface, say IMyIterator, you can write "normal", non-generic algorithms on it

void myalgorithm(IMyIterator* iter) {...}

The great difference in implementing these two functions is the different level of language and IDE support of working with interfaces vs generic types. Another point is that not everyone is that familiar with template programming, as probably you are. Finally, the second case allows to hide the implementation, while the first one is header-only. So please don't tell me that there is no difference between them, unless you can show me where I am wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

浮生面具三千个 2024-10-22 11:32:50

您可以使用SCARY迭代器。数据库是 SCARY 迭代擅长的示例之一。

然而,C# 的 IEnumerable 实际上与 STL 迭代没有任何不同,只是涉及的多态性是在运行时而不是编译时。在其之上编写自己的运行时多态性应该不会太难。

You could use SCARY iterators. A database is one of the examples given of where SCARY iteration excels.

However, C#'s IEnumerable<T> is not really any different to an STL iteration, except that the polymorphism involved is at run-time, not compile-time. It shouldn't be tooo hard to write your own run-time polymorphism on top of it.

滥情稳全场 2024-10-22 11:32:50

另一种替代方案的示例是 Qt4 引入 Java 样式迭代器

QList<int> list;
...
QListIterator<int> i(list);
while (i.hasNext())
    sum += i.next();

One example of an alternative would be Qt4 introducing Java-Style iterators:

QList<int> list;
...
QListIterator<int> i(list);
while (i.hasNext())
    sum += i.next();
你怎么敢 2024-10-22 11:32:50

您还可以看到 Boost::FOR_EACH 它简化了迭代的语法。

you can also see Boost::FOR_EACH it simplify the syntax of iteration.

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