学习自唐佐林老师 Qt
实验1 QMap使用
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QMap>
#include <QMapIterator>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QMap<QString, int> map;
map.insert("key 2", 2);
map.insert("key 1", 1);
map.insert("key 0", 0);
//打印键
QList<QString> kList = map.keys();
for(int i=0; i<kList.count(); i++)
{
qDebug() << kList[i];
}
//打印值
QList<int> vList = map.values();
for(int i=0; i<vList.count(); i++)
{
qDebug() << vList[i];
}
QMapIterator<QString, int> it(map);
//打印键值对
while( it.hasNext() )
{
it.next();
qDebug() << it.key() << " : " << it.value();
}
return a.exec();
}
实验2 QHash使用
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QHash>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QHash<QString, int> hash;
hash.insert("key 2", 2);
hash.insert("key 1", 1);
hash.insert("key 0", 0);
//打印键
QList<QString> kList = hash.keys();
for(int i=0; i<kList.count(); i++)
{
qDebug() << kList[i];
}
//打印值
QList<int> vList = hash.values();
for(int i=0; i<vList.count(); i++)
{
qDebug() << vList[i];
}
hash["key 4"] = 4;
QHash<QString, int>::const_iterator i;
//打印键值对
for(i=hash.constBegin(); i!=hash.constEnd(); ++i)
{
qDebug() << i.key() << " : " << i.value();
}
return a.exec();
}