最近在项目开发的过程中,有用到键-值数据存储结构,QMap和QHash都可以使用,这两个都可以存储键值对数据,那么具体用哪种呢,,本篇记录这两个类的使用方法。
网上的资料显示主要从这三个方面进行对比:
1.从数据结构上来说,QMap是基于红黑树存储,所以保证元素的有序,而QHash是基于哈希表的,没有固定的顺序。
2.从内存方面来说,由于 红黑树需要额外的内存来维护树的结构,所以QMap比QHash占用更多的内存。
3.从查找效率上来说,由于红黑树的特性,QMap在查找操作上具有较好的性能,而通过哈希函数计算索引,查找速度通常更快。
下面通过代码实例来对比一下:
#include <QCoreApplication>
#include <QDebug>
#include <QMap>
#include <QHash>
void QMapSequence()
{
QMap<QString, int> map;
// 插入元素
map.insert("Tom", 175);
map.insert("Camel", 174);
map.insert("Scott", 178);
// 遍历元素(按键值排序)
for (auto it = map.begin(); it != map.end(); ++it) {
qDebug() << it.key() << ":" << it.value();
}
// 查找元素
if (map.contains("Camel")) {
qDebug() << "Camel's height:" << map.value("Camel");
}
// 删除元素
map.remove("Scott");
}
void QHashSequence()
{
QHash<QString, int> hash;
// 插入元素
hash.insert("Tom", 175);
hash.insert("Camel", 174);
hash.insert("Scott", 178);
// 遍历元素(按键值排序)
for (auto it = hash.begin(); it != hash.end(); ++it) {
qDebug() << it.key() << ":" << it.value();
}
// 查找元素
if (hash.contains("Camel")) {
qDebug() << "Camel's height:" << hash.value("Camel");
}
// 删除元素
hash.remove("Scott");
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QMapSequence();
qDebug() << "\n";
QHashSequence();
return a.exec();
}
运行结果:
1.插入数据对比:
#include <QCoreApplication>
#include <QDebug>
#include <QDateTime>
#include <QMap>
#include <QHash>
#include <QString>
struct Student
{
QString name; //姓名
QString number; //学号
int age; //年龄
};
QMap<QString, Student> m_map;
QHash<QString, Student> m_hash;
const int count = 100; //数量
void QMapInsert()
{
qint64 start = QDateTime::currentMSecsSinceEpoch();
qDebug() << "QMap insert=======start===" << start;
for(int i = 0; i < count; i++)
{
Student stu;
stu.age = i;
stu.name = QString("Scott%1").arg(i);
stu.number = QString::number(1000+i);
m_map.insert(stu.number, stu);
}
qint64 end = QDateTime::currentMSecsSinceEpoch();
qDebug() << "QMap insert=======end=====" << end;
qDebug() << "QMap count================" << m_map.count() << " use time = " << (end - start);
}
void QHashInsert()
{
qint64 start = QDateTime::currentMSecsSinceEpoch();
qDebug() << "QHash insert=======start===" << start;
for(int i = 0; i < count; i++)
{
Student stu;
stu.age = i;
stu.name = QString("Scott%1").arg(i);
stu.number = QString::number(1000+i);
m_hash.insert(stu.number, stu);
}
qint64 end = QDateTime::currentMSecsSinceEpoch();
qDebug() << "QHash insert=======end=====" << end;
qDebug() << "QHash count================" << m_hash.count() << " use time = " << (end - start);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QMapInsert();
qDebug() << "\n";
QHashInsert();
return a.exec();
}
运行结果:
从以上可以看出QHash插入元素明显示比‘QMap快,
2.查找元素。
void QMapFind()
{
qint64 start = QDateTime::currentMSecsSinceEpoch();
qDebug() << "QMap find=========start===" << start;
// 查找元素
if (m_map.contains("5000")) {
qDebug() << "5000's name====:" << m_map.value("5000").name;
}
qint64 end = QDateTime::currentMSecsSinceEpoch();
qDebug() << "QMap find=========end=====" << end;
qDebug() << "QMap count================" << m_map.count() << " use time = " << (end - start);
}
void QHashFind()
{
qint64 start = QDateTime::currentMSecsSinceEpoch();
qDebug() << "QHash find=========start===" << start;
// 查找元素
if (m_hash.contains("5000")) {
qDebug() << "5000's name====:" << m_hash.value("5000").name;
}
qint64 end = QDateTime::currentMSecsSinceEpoch();
qDebug() << "QHash find=========end=====" << end;
qDebug() << "QHash count================" << m_hash.count() << " use time = " << (end - start);
}
运行结果:
看这样子差别一样,查找速度都很快。
3.删除元素
void QMapDelete()
{
qint64 start = QDateTime::currentMSecsSinceEpoch();
qDebug() << "QMap delete=======start===" << start;
// 删除元素
m_map.remove("5000");
qint64 end = QDateTime::currentMSecsSinceEpoch();
qDebug() << "QMap delete=======end=====" << end;
qDebug() << "QMap count================" << m_map.count() << " use time = " << (end - start);
}
void QHashDelete()
{
qint64 start = QDateTime::currentMSecsSinceEpoch();
qDebug() << "QHash delete=======start===" << start;
// 删除元素
m_hash.remove("5000");
qint64 end = QDateTime::currentMSecsSinceEpoch();
qDebug() << "QHash delete=======end=====" << end;
qDebug() << "QHash count================" << m_hash.count() << " use time = " << (end - start);
}
运行结果:
删除效果一样
参考:
5.2 QMap 与 QHash 的功能详解、应用示例与比较-优快云博客
https://zhuanlan.zhihu.com/p/670311393