简体   繁体   English

Qt框架中的QMap与QList类

[英]QMap vs QList Class in the Qt framework

I'm trying to decide on whether to use the QList or QMap class in some of my future Qt projects. 我试图决定在将来的某些Qt项目中使用QList还是QMap类。 In order to determine the best choice for me, I'd like to determine some of their similarities and some of their differences in order to understand what works best in certain instances. 为了确定最适合我的选择,我想确定它们的某些相似之处和不同之处,以便了解在某些情况下最有效的方法。 Is my understanding of these similarities and differences correct? 我对这些异同的理解正确吗?

Similarities: 相似点:

  • Both are containers 都是容器

  • Both contain unordered data 两者都包含无序数据

Differences: 区别:

  • QMap has key value pairs whilst QList only has values QMap具有键值对,而QList仅具有值

  • QMap uses a hash function to place values in the appropriate index whilst QList simply appends the entries QMap使用哈希函数将值放置在适当的索引中,而QList只是追加条目

Are there any more items of similarities and differences? 还有更多相似和不同之处吗?

I could look at the generic computer science definitions but I read somewhere there could be nuanced differences in the Qt framework. 我可以看一下通用的计算机科学定义,但我读到某处Qt框架可能存在细微差别。

QList and QMap differ in the way the data is being organized. QListQMap的数据组织方式不同。 This results in different performance and slightly different memory consumption (for most use cases the latter usually doesn't matter). 这导致不同的性能和稍微不同的内存消耗(在大多数情况下,后者通常无关紧要)。 You can find the computational complexity in the Qt documentation . 您可以在Qt文档中找到计算复杂性 If you are storing a lot of elements this might make a big difference. 如果要存储很多元素,则可能会有很大的不同。 Think about how frequently you want to access the data when selecting a container (searching vs. inserting vs. deleting). 考虑选择容器时要多久访问一次数据(搜索vs.插入vs.删除)。

[Keep in mind, though, that algorithmic complexity is a theoretical property that is only useful for large n. [但是请记住,算法复杂度是仅对大n有效的理论属性。 In practice a linear search through an array with a small number of elements (<1,000) often outperforms lists/trees due to locality of reference . 实际上,由于引用的局部性,通过具有少量元素(<1,000)的数组进行线性搜索通常优于列表/树。 If you care about performance don't guess, always measure.] 如果您不关心性能,请不要猜测,请务必进行评估。]

Both contain unordered data 两者都包含无序数据

That's actually not true for QMap . 对于QMap实际上并非如此。 QMap is implemented as a self-balancing binary search tree which is a sorted data structure. QMap被实现为自平衡二进制搜索树 ,它是一种排序的数据结构。

BTW: You can often implement your code in a generic way that makes it easy to switch to another container type later (eg if the access pattern changes or your assumptions turn out to be wrong). 顺便说一句:您通常可以以一种通用的方式来实现您的代码,这使得以后可以轻松切换到另一个容器类型(例如,如果访问模式更改或您的假设被证明是错误的)。 Using auto can help making this painless. 使用auto可以帮助您减轻痛苦。

I think you made some mistakes about QMap . 我认为您对QMap犯了一些错误。

The QMap keeps its content always sorted by key. QMap始终保持其内容按键排序。 See the Documentation here . 请参阅此处文档 So it is not unordered as you mentioned. 因此,它并非像您提到的那样无序。

Then a QMap does not use a hash function. 然后, QMap不使用哈希函数。 It stores elements by comparing them with operator<() . 它通过将元素与operator<()进行比较来存储元素。

In fact, you a confusing QMap and QHash . 实际上,您会混淆QMapQHash The QHash is indeed arbitrarily ordered and its elements needs to provide an operator==() for the comparison and a qHash(key) function. QHash确实是任意排序的,并且其元素需要提供用于比较的operator==()qHash(key)函数。

I think it can help you to better understand what you need to use. 我认为它可以帮助您更好地了解需要使用的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM