Differences between QMap and QHash classes, application scenarios, advantages and disadvantages

QMap and QHash are container classes used to store key-value pairs in the Qt framework. They have the following differences, application scenarios, advantages and disadvantages:

the difference:

  1. Data structure: QMap is implemented based on red-black trees, ensuring that elements are ordered; QHash is implemented based on hash tables, and there is no fixed order.
  2. Memory consumption: Since red-black trees require additional memory to maintain the tree structure, QMap takes up more memory than QHash.
  3. Search efficiency: Due to the characteristics of red-black trees, QMap has better performance in search operations; while QHash calculates the index through a hash function, and the search speed is usually faster.

Comparison of time complexity between the two

Container class

key lookup

insert

average

worst

average

worst

QMap

O(log n)

O(log n)

O(log n)

O(log n)

QHash

Death.O(1)

O(n)

Death.O(1)

O(n)

4 QMap class

  QMap<Key, T> provides a mapping from keys of type Key to values ​​of type T.

5 QHash class

  QHash<Key, T> has almost the same API as QMap. QHash maintains a hash table (Hash Table), and the size of the hash table is adapted to the number of QHash data items.

6 Java style iterator traversing a container

  For each container class, Qt provides two types of Java-style iterator data types, namely read-only access and read-write access. The specific classification is as follows:

#include <QDebug>
int main(int argc,char *argv[])
{
    QMap<QString, QString> map;                                         // 定义一个关联容器

    map.insert("beijing", "111");                                       // 添加内容 对应 键值 - 内容
    map.insert("shanghai", "021");
    map.insert("nanjing", "025");

    QMapIterator<QString,QString> i(map);                               // 为遍历容器定义变量

    for(;i.hasNext();)
        qDebug()<<"  "<<i.key()<<"  "<<i.next().value();

    QMutableMapIterator<QString,QString> mi(map);

    if(mi.findNext("111"))                                              // 按内容查找 查找到内容为 "111" 替换为 "010"
        mi.setValue("010");

    QMapIterator<QString,QString> modi(map);

    qDebug()<<"  ";
    for(;modi.hasNext();)
        qDebug()<<" "<<modi.key()<<"  "<<modi.next().value();

    return 0;
}

 The running results are as follows:

7. STL style iterator traverses the container

  For each container class, Qt provides two types of STL style iterator data types, namely read-only access and read-write access. The specific classification is as follows:

#include <QDebug>
int main(int argc,char *argv[])
{
    QMap<QString,QString> map;

    map.insert("beijing", "111");
    map.insert("shanghai", "021");
    map.insert("nanjing", "025");

    QMap<QString,QString>::const_iterator i;                // 遍历 打印一遍原始信息
    for(i=map.constBegin();i!=map.constEnd();++i)
        qDebug()<<"  "<<i.key()<<"  "<<i.value();

    QMap<QString,QString>::iterator mi;                     // 查找 按键值进行查找 替换键值为beijing的内容
    mi=map.find("beijing");
    if(mi!=map.end())
        mi.value()="010";

    QMap<QString,QString>::const_iterator modi;
    qDebug()<<"  ";
    for(modi=map.constBegin();modi!=map.constEnd();++modi)
        qDebug()<<"  "<<modi.key()<<"  "<<modi.value();

    return 0;
}

  The running results are as follows:

Application scenarios:

  1. QMap is suitable for situations where you need to sort by key or perform range queries. For example, dictionary, associative array, etc.
  2. QHash is suitable for quickly searching, inserting and deleting large amounts of data. Such as caching, indexing, etc.

Advantages and Disadvantages:

QMap:

  1. Advantages: Provides the function of sorting by key, supports range queries, and has better performance in certain scenarios;
  2. Disadvantages: Compared with QHash, it takes up more memory space, and insertion/deletion operations may be slightly slower.

QHash:

  • Advantages: Better search, insertion and deletion performance in most cases, taking up less memory;
  • Disadvantages: There is no fixed order and no range query is supported.

Benefits of this article, receive free Qt development learning materials package and technical videos, including (Qt practical project video tutorial + code, C++ Language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project practice, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓ ↓↓↓See below↓↓Click at the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/hw5230/article/details/134785905