QMap

 

#include "mainwindow.h"
#include <QApplication>
#include <QMap>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMap<QString,int> map;
    //QMap<Key,T> 键 值
        map.insert("eins o",1);
        map.insert("sieben",7);
        map["a test"]=23;
        map["a test"]=13;  //重新定义“值”
        int val=map.value("a test");
        qDebug()<<val;

        //value仅在原先无值情况下赋值,否则只是作为返回值用
        int val2=map.value("error");
        qDebug()<<val2;    ///0
        int val3=map.value("error2",20);
        qDebug()<<val3;
        int val4=map.value("a test",40);
        qDebug()<<val4;
        qDebug()<<"1*************************************************************************1";
        using an Stl-style iterator
        QMap<QString,int>::ConstIterator i=map.constBegin();  //constBegin():Returns a const STL-style iterator pointing to the first item in the map.
        while(i!=map.constEnd())
        {
            qDebug()<<i.key()<<": "<<i.value();  //对应key value值
            ++i;
        }
         qDebug()<<"2*************************************************************************2";
        QMap<QString,int>::Iterator it;
        it=map.find("sieben");
        if(it!=map.end())
            it.value()=8;

        for(QMap<QString,int>::ConstIterator ite=map.constBegin(); ite!=map.constEnd(); ++ite)
            qDebug()<<ite.key()<<": "<<ite.value();
         qDebug()<<"3*************************************************************************3";
            it=map.end();  //map以“ ”结束
            qDebug()<<it.key()<<": "<<it.value();
    return a.exec();
}

输出结果:

13 
0 
20 
13 
1*************************************************************************1 
"a test" :  13 
"eins o" :  1 
"sieben" :  7 
2*************************************************************************2 
"a test" :  13 
"eins o" :  1 
"sieben" :  8 
3*************************************************************************3 
"" :  1896193535 

QMap<Key,T>是Qt容器类型的一种,它通过(Key, value)存储一对值,并通过Key可以查找与之关联的value的值。

QMapQHash是很相似的,不同的地方是: 
QHash的查找速度比QMap要快很多。 
- 在对QHash进行迭代时,这些项是任意排序的。在QMap中,项总是按键排序。 
QHash的关键类型必须提供运算符==()和全局QHash(key)函数。QMap的关键类型必须提供操作符<(),以指定全序顺序。从Qt 5.8.1开始,使用指针类型作为键也是安全的,即使底层操作符<()不提供全序关系。


这里有一个带有QString类型关键字和 int类型值的QMap示例:

QMap<QString,int> map;
  •  

你可以用运算符[ ]插入一对 (key,value) 到QMap对象中:

//依次插入了三对值,("one",1), ("three",3),("seven",7)
map["one"] = 1;
map["three"] = 3;
map["seven"] = 7;

除了使用运算符[ ]外,还可以使用函数insert() 插入

map.insert("twelve",12);        //插入("twelve",12)   

如果想要查询QMap对象中的值,使用运算符[ ] 或者函数 value()

//通过关键字tirteen查找对应的值
int num1 = map["thirteen"];
int num2 = map.value("thirteen");

查看QMap对象中是否包含某一项,使用函数contains() //存在返回true,否则,返回false

int timeout = 30;
if(map.contains("TIMEOUT))
    timeout = map.value("TIMEOUT");

假设QMap对象中 TIMEOUT 关键字不存在

int timeout = map.value("TIMEOUT",30);
//value()函数返回第二个参数30

通常,我们建议使用contains()value()而不是操作符[]()来查找QMap对象中的“键”,通过“键”来查找“value”值。原因是,如果QMap对象中,不存在要查找的关键项,那么操作符[]()会在QMap对象中自动地插入一个项。 
例如。在这个例子中,将会创建1000个项在QMap对象中

QMap<int, QWidget *> map;
...
for(int i = 0; i < 1000 ; ++ i){
    if(map[i] == okButton)
        cout << "Found button at index " << i << endl;
        }

要遍历QMap对象中的所有项,可以使用 迭代器(iterator).QMap提供了java风格的迭代器(QMapIteratorQMutableMapIterator)和STL样式的迭代器(QMap::const_iterator 和 QMap::iterator)。

**如何使用STL样式的迭代器

QMap<QString, int>::const_iterator i = map.constBegin();
while (i != map.constEnd()) {
      cout << i.key() << ": " << i.value() << endl;

通常,QMap只允许每个键对应一个值。如果在QMap中有一个已经存在的键调用insert(),那么前面的值将被删除。例如:

map.insert("plenty",100);
map.insert("plenty",2000);
//"plenty"对应的值100,修改为2000

如果想让一个键对应多个值,可以通过调用函数insertMulti()实现(或者QMultiMap类实现). 
检索一个键对应的所有值,使用函数values(const Key &key) ,这个函数返回一个QList类模板。

QList<int> values = map.values("plenty");
for(int i = 0; i < values.size(); ++i)
    cout <<values.at(i) << endl;

另一种方法是调用find()来获取第一个项的STL样式迭代器,并从获取的迭代器开始迭代。

QMap<QString, int>::iterator i  = map.find("plenty");
while( i != map.end() && i.key() == "plenty)
{
    cout << i.value() << endl;
    ++i
}

如果你只需要从QMap对象中查看值(不是键),可以使用foreach循环

QMap<QString,int> map;
..
foreach( int value, map)
    cout <<value << endl;

插入的项也可以从QMap对象中删除。 
第一种方法是:调用remove() //int QMap::remove(const Key &key) 函数,这将删除给定键的所有相关项。 
第二种方法是:调用QMutableMapIterator::remove()。 
第三种方法是:调用clear(),清空了QMap对象。


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值