C++ 为什么使用 QVector(Qt) 而不是 std::vector

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5623218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:33:30  来源:igfitidea点击:

Why use QVector(Qt) instead of std::vector

c++qtvector

提问by Athiwat Chunlakhan

I'm very new to C++ and Qt, but I'm very good at C#/Java.

我对 C++ 和 Qt 很陌生,但我很擅长 C#/Java。

The point is I like cross-platform, but I'm confuse with Qt. Isn't std::vectoralready cross-platform, doesn't Qt provide an equivalent to a non-crossplatform thing?

关键是我喜欢跨平台,但我对 Qt 感到困惑。是不是std::vector已经跨平台了,Qt 不提供与非跨平台事物等效的东西吗?

Also how are Fileand QFiledifferent?

还有怎么FileQFile不同?

A link would be nice, thanks :)

一个链接会很好,谢谢:)

采纳答案by Nawaz

This article loooks good. It compares Qt Template Library with Standard Template Library:

这篇文章看起来不错。它将 Qt 模板库与标准模板库进行了比较:

Hope, you'll find it interesting seeing all the differences listed there in the article.

希望您会发现看到文章中列出的所有差异会很有趣。

EDIT:

编辑:

Here is what I find interesting:

以下是我觉得有趣的地方:

My opinion is that the biggest advantage of the QTL is that it has the same implementation (including binary compatibility) on all OSes supported by Qt. Some STL implementations might be below par when it comes to performance or they might be missing functionality. Some platforms don't even have an STL! On the other hand, the STL is more customizable and is available in its entirety in header files… Like I said, there is no clear winner.

我认为QTL最大优点是它在 Qt 支持的所有操作系统上具有相同的实现(包括二进制兼容性)。某些 STL 实现在性能方面可能低于标准,或者它们可能缺少功能。有些平台甚至没有 STL!另一方面,STL 更具可定制性,并且在头文件中完整可用……就像我说的,没有明显的赢家

Like he said, no clear winner. But still reading the article makes lots of things clear. Its better to know the difference than going for one, without knowing the other.

就像他说的,没有明确的赢家。但是仍然阅读这篇文章会使很多事情变得清晰。知道其中的区别比不了解另一个要好。

回答by Michael Burr

The QVector class is reference counted and is geared to being shared without copying. Qt provides a lot of containers that correspond to STL containers. A document that describes these with some explanation of the internals and a bit of rationale:

QVector 类是引用计数的,适用于无需复制即可共享。Qt 提供了很多与STL 容器相对应的容器。一份描述这些内容的文档,并附有对内部结构的一些解释和一些基本原理:

回答by phimuemue

From over here:

这里开始

Qt originates from a time when C++ and the standard library were not standardized or well supported by compilers. It therefore duplicates a lot of stuff that is now in the standard library, such as containers and type information. Most significantly, they modified the C++ language to provide signals, so that Qt classes can not be used easily with non-Qt classes.

Qt 起源于 C++ 和标准库没有标准化或编译器没有很好支持的时代。因此,它复制了标准库中的许多内容,例如容器和类型信息。最重要的是,他们修改了 C++ 语言以提供信号,因此 Qt 类不能轻易与非 Qt 类一起使用。

回答by John Dibling

C++'s std::vectoris cross-platform because it is part of the C++ Standard. Every C++-conformant compiler must provide it.

C++std::vector是跨平台的,因为它是 C++ 标准的一部分。每个符合 C++ 的编译器都必须提供它。

I'm not familiar with Qt, but I did see this in the docs:

我不熟悉 Qt,但我确实在文档中看到了这一点:

Note: All functions in this class are reentrant.

注意:该类中的所有函数都是可重入的。

It's also likely (speculation) that the QVector class is more easily integrated to hold Qt-centric objects than std::vectormight be. Again, I'm not familiar with Qt so you have to decide for yourself.

也可能(推测)QVector 类比可能更容易集成以保存以 Qt 为中心的对象std::vector。同样,我不熟悉 Qt,所以你必须自己决定。

As a rule of thumb (to which there are many exceptions), I would tend to use std::vectorunless I had a compelling reason to use some library-specific container class.

根据经验法则(有很多例外),std::vector除非我有令人信服的理由使用某些特定于库的容器类,否则我会倾向于使用。

回答by user1095108

The bad experience I've had with QTLwas related to QTLnot raising any exceptions; this makes it harder to trace and fix critical errors. Also, STLimplementations are closely related to a compiler, because parts of the library require compiler-specific extensions to the language. This means a STLimplementation can often outperform QTL, which needs to be portable and therefore cannot benefit from said extensions. The debugging issue was critical for me though.

我的糟糕经历QTLQTL没有提出任何异常有关;这使得跟踪和修复严重错误变得更加困难。此外,STL实现与编译器密切相关,因为库的某些部分需要对语言进行特定于编译器的扩展。这意味着一个STL实现通常可以胜过QTL,它需要是可移植的,因此不能从所述扩展中受益。不过,调试问题对我来说至关重要。

回答by dtech

Since no answer mentioned it, Qt containers, including QVectorgenerally have a fuller API, which does enable a certain amount of extra convenience and reduces verbosity when compared to std::vector.

由于没有答案提到它,Qt 容器,包括QVector通常具有更完整的 API,与std::vector.

QVectorisn't really integrated into the Qt APIs, that role is taken by misfit QList, so it is not really a strong argument to use QVectorfor overall better compatibility with Qt APIs. Note that this might change for Qt 6, as the shortcomings of QListbecome more and more acknowledged.

QVector并没有真正集成到 Qt API 中,该角色由 misfit 承担QList,因此使用它来QVector与 Qt API 整体更好的兼容性并不是一个强有力的论据。请注意,随着 Qt 6 的缺点QList越来越多,这种情况可能会改变。

That being said, if you already depend on Qt for your application, it would make good sense to use QVectorfor the convenience. I presume that nobody is going to add such a bloated dependency as Qt just for a container or two. QVectoris efficient and a solid performer, and will run without problems on any platform, supported by Qt.

话虽如此,如果您的应用程序已经依赖于 Qt,那么QVector为了方便而使用它是很有意义的。我认为没有人会为一两个容器添加像 Qt 这样臃肿的依赖项。QVector高效且性能稳定,在 Qt 支持的任何平台上运行都不会出现问题。

On the other hand, if you want to make a core logic API that is framework agnostic, it would be a good idea to develop it in standard C++ if possible, so you get something portable that isn't tied to a particular GUI framework so you can easily migrate it to a different one in the future if you need to.

另一方面,如果你想制作一个与框架无关的核心逻辑 API,如果可能的话,最好用标准 C++ 开发它,这样你就会得到一些不依赖于特定 GUI 框架的可移植的东西,所以如果需要,您可以在将来轻松地将其迁移到另一个。