一、QMap类
QMap<Key,T>提供一个从类型为Key的键到类型为T的值的映射。通常,QMap 存储的数据形式是一个键对应一个值,并且按照键Key的次序存储数据。为了能够支持一键多值的情况,QMap提供QMap<Key,T>:;insertMulti(和QMap<Key,T>:values()函数。
#include <QCoreApplication>
#include<QMap>
#include <QDebug>
//打印调试信息
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// QMap类
//1、创建QMap实例,第一个参数为 QString类型的键,第二个参数为int类型的值
QMap<QString,int>qmap;
//插入数据信息,它有两种方式进行操作
qmap["chinese"]=119;
qmap["english"]=120;
qmap.insert("math",115);
qmap.insert("Physics",99);
qmap.insert("Chemistry",100);
qDebug()<<qmap<<endl;
//删除数据信息key键
qmap.remove("Chemistry");
qDebug()<<qmap<<endl;
//遍历QMap类的实例:数据信息
//1、迭代器
QMapIterator<QString,int> itr(qmap);
while(itr.hasNext())
{
itr.next();
qDebug()<<itr.key()<<":"<<itr.value()<<endl;
}
qDebug()<<endl;
//2、STL类型的迭代
QMap<QString,int>::const_iterator stritr=qmap.constBegin();
while(stritr!=qmap.constEnd())
{
qDebug()<<stritr.key()<<":"<<stritr.value()<<endl;
stritr++;
}
//key键/T键-->来查找
qDebug()<<"根据key来查找"<<qmap.value("Math");
qDebug()<<"根据T来查找"<<qmap.key(99);
//修改键值
//一个键对应一个值,再次调用insert()函数将覆盖之前的值
qmap.insert("Math",118);
qDebug()<<qmap.value("Math");
//查询是否包含某一个键
qDebug()<<"result"<<qmap.contains("chinese");
qDebug()<<"result"<<qmap.contains("Chemistry");
//输出所有QMap实例化:key键和T值
qDebug()<<endl;
QList<QString> akeys=qmap.keys();
qDebug()<<akeys;
QList<int> aValues =qmap.values();
qDebug()<<aValues;
//一个键对应多个值
//直接使用QM
qDebug()<<endl;
QMultiMap<QString,QString> mulmap;
mulmap.insert("student","id");
mulmap.insert("student","age");
mulmap.insert("student","name");
mulmap.insert("student","high");
qDebug()<<mulmap;
return a.exec();
}
二、QHash 类
QHash<Key,T>具有与QMap几乎完全相同的API.QHash维护着一张哈希表(Hash Table),哈希表的大小与QHash的数据项的数目相适应。QHash以任意的顺序组织它的数据。当存储数据的顺序无关紧要时,建议使用 QHash作为存放数据的容器。
#include <QCoreApplication>
#include<QMap>
#include <QDebug>
//打印调试信息
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// QHash类
QHash<QString,int> qhash;
qhash["key 1"]=3;
qhash["key 1"]=8;
qhash["key 4"]=4;
qhash["key 2"]=2;
qhash.insert("key 3",30);
QList<QString>list=qhash.keys();
for(int i=0;i<list.length();i++)
qDebug()<<list[i]<<","<<qhash.value(list[i]);
//QHash内部的迭代器QHashIterator类
QHash<QString,int>hash;
hash["key 1"]=33;
hash["key 2"]=44;
hash["key 3"]=55;
hash["key 4"]=66;
hash.insert("key 3",100);
QHash<QString,int>::const_iterator iterator;
for(iterator=hash.begin();iterator!=hash.end();iterator++)
qDebug()<<iterator.key()<<"--->"<<iterator.value();
return a.exec();
}
QMap与 QHash 区别:
QHash与QMap的功能差不多,但QHash的查找速度更快;
QMap是按照键的顺序存储数据,而QHash 是任意顺序存储的;
QMap的键必须提供*<”运算符,而QHash的键必须提供“=="运算符和一个名为 qHash0的全局散列函数。
三、QVector 类
QVector<T>在相邻的内存中存储给定数据类型T的一组数值。在一个QVector 的前部或者中间位置进行插入操作的速度是很慢的,这是因为这样的操作将导致内存中的大量数据被移动,这是由QVector存储数据的方式决定的。
#include <QCoreApplication>
#include<QMap>
#include <QDebug>
//打印调试信息
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// QVector类
//QVector<T>是Qt的一个容器类
QVector<int>qvr;
//第一种赋值方式
qvr<<10;
qvr<<20;
qvr<<30;
//第二种赋值方式
qvr.append(50);
qvr.append(60);
qvr.append(70);
qvr.append(80);
qDebug()<<qvr<<endl;
//求出QVector类容器的实例化:元素个数
qDebug()<<"元素个数:"<<qvr.count()<<endl;
//遍历所有元素
qDebug()<<endl;
for(int i=0;i<qvr.count();i++)
qDebug()<<qvr[i];
//删除qvr内的元素
qDebug()<<endl;
qvr.remove(0);
qDebug()<<endl;
for(int i=0;i<qvr.count();i++)
qDebug()<<qvr[i];
qvr.remove(2,3);//从下标2开始删,删除3个数字
qDebug()<<endl;
for(int i=0;i<qvr.count();i++)
qDebug()<<qvr[i];
qDebug()<<endl;
//判断容器是否包含某个元素
qDebug()<<"result="<<qvr.contains(80)<<endl;
return a.exec();
}