哪个 C++概念更适合对数据库查询进行建模 - 流还是输入迭代器?

发布于 2024-12-26 02:27:12 字数 292 浏览 1 评论 0原文

我想将数据库查询视为标准 C++ 输入迭代器。 另一方面,可以将数据库查询视为查询结果项的输入流。 您认为更好的数据库查询模型是输入迭代器还是输入流?

就我个人而言,我的印象是 C++ IO 流应该仅对字符进行操作,但我从未见过任何字符不是 charwchar_t 的流示例。代码>.据我所知,流的模板化性质允许我将任何内容作为字符传递,因此从理论上讲,我似乎可以将查询结果项视为字符以便进行流式处理,但我不确定它是否是一个好主意。

欢迎提出建议。

谢谢。

I would like to treat a database query as a standard C++ input iterator.
On the other hand, one can view a database query as an input stream of query result items.
What do you think is a better model for a db query - an input iterator or an input stream?

Personally, I have an impression that C++ IO streams are supposed to operate on characters only, where I have never seen any example of a stream where characters would be something other than char or wchar_t. I understand, that the templated nature of the streams allows me to pass anything as a character, so theoretically, it seems that I can treat the query result item as a character for the sake of streaming, but I am not sure if it is a good idea.

Advices are welcome.

Thanks.

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

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

发布评论

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

评论(1

无法回应 2025-01-02 02:27:12

您用作流字符类型的任何内容都需要具有字符特征,也许还需要一些与语言环境有关的东西,因为有人可能会尝试将语言环境注入您的流中。这可能是无稽之谈,但它仍然存在于界面中,即使您将其设置为错误,您也可能需要将其设置为明智的错误。

我肯定会使用输入迭代器,在 C++ 中,它是对象序列的简单模型。

流除了呈现序列之外,还可以做很多其他事情(格式化、streambuf 的控制、神秘的错误状态模型)。其中大部分内容可能不适用于您的数据库项目,尽管我认为其中一些可能适用。例如,控制数据库查询结果流的缓冲区大小是有意义的,但从中进行格式化读取则没有意义。

istream_iterator 存在的事实证明,即使您为某人提供流,他们也可能更喜欢/需要迭代器接口。

Anything you use as a stream character type needs to have character traits, and maybe some stuff to do with locales, since someone might try to imbue a locale into your stream. Which might be nonsense, but it's still there in the interface and even if you make it an error you probably need to make it a sensible error.

I'd definitely use an input iterator, in C++ it's the simple model for a sequence of objects.

Streams do a lot of other stuff as well as merely presenting a sequence (formatting, control of the streambuf, the arcane error state model). Much of that probably isn't applicable to your database items, although I suppose some of it could be. For example controlling the buffer size of a DB query result stream would make sense, but formatted reads from it wouldn't.

The fact that istream_iterator exists is proof that even if you offer someone a stream, they might well prefer/need the iterator interface.

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