首页 > 技术知识 > 正文

qmap 的使用:

QMap<QString, int> map;//键是string,int     map[“key 2”] = 2;       //以数组的形式存储QMap     map[“key 0”] = 0;     map.insert(“key 1”, 1);     //排序是根据key排序的。     map.insert(“key 1”, 4);     //前面已经有key  1, 所以这里会更新key的value值为4.          QList<QString> list = map.keys();//将map里面的key获取出来,放在list里面     for(int i=0; i<list.count(); i++)     {         qDebug() << list[i];        //map升序排序,打印排序好的key0,key 1, key 2、     }     QList<int> vlist = map.values();    //这里vlist里面存放的是int类型的value     for(int i=0; i<vlist.count(); i++)     {         qDebug() << vlist[i];       //0, 1, 2     }     QMapIterator<QString, int> it(map);    //it指向map中第一个元素之前的位置。     while(it.hasNext())     {         it.next();         qDebug() << it.key() << “:::” << it.value();     }

qhash的使用:

QHash<QString, int> hash;       //以键值对的方式存取。     hash[“key 2”] = 2;     hash[“key 0”] = 0;     hash.insert(“key 1”, 1);     QList<QString> list = hash.keys();     for(int i=0; i<list.count(); i++)     {         qDebug() << list[i];        //QHash无排序     }     QList<int> vlist = hash.values();     for(int i=0; i<vlist.count(); i++)     {         qDebug() << vlist[i];   //打印0, 1, 2     }     QHash<QString, int>::const_iterator i;     for(i = hash.constBegin(); i!=hash.constEnd(); ++i)     {         qDebug() << i.key() << “:” << i.value();     }

猜你喜欢