我的数据结构定义为QMap ,如何按值对它进行排序?

问题描述 投票:1回答:3

我有一个随机生成的整数列表,并用这些值填充了QMap,但我想按值对QMap进行排序

qt4.8
3个回答
4
投票

这是在qt c ++中如何按值而不是按键对QMap进行排序的演示。提取QMap的值并将其存储在QList容器对象中,然后通过qSort方法进行排序。密钥也自己存储在QList中。排序完成后,将清除QMap对象,然后将键和值按值从小到大的顺序重新插入QMap容器​​中。请参阅下面的解决方案:

#include <QCoreApplication>
#include <qalgorithms.h>
#include <QMap>
#include <QDebug>
#include <QList>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QMap <int, int> dataMapList;
    //QMap <int, int> sorted = new QMap<int, int>();
    QList <int> keys; // container to store all keys from QMap container
    QList<int> values; // container to store all values from QMap container
    QMap<int, int>::Iterator h; // used to loop/ iterate through QMap

    // used to iterate through QLists
    QList<int>::Iterator i; //
     QList<int>::Iterator j;

     //inserts to QMap Container
    dataMapList.insert(1,34);
    dataMapList.insert(3,2);
    dataMapList.insert(2,32);
    dataMapList.insert(14,89);
    dataMapList.insert(7,23);

    h=dataMapList.begin();

     qDebug()<< "unsorted";
    //list out the unsorted values along with their respective keys
    while(h!=dataMapList.end()){
        qDebug() << "[" << h.key()<<"], " <<"[" <<h.value()<<"]" << endl;
        h++;
    }


    values = dataMapList.values(); // pass all values in the QMap to a QList container to store values only
    keys= dataMapList.keys(); // pass all keys in the QMap to a QList container to store already sorted by default keys

    qSort(values); // sorts the values in ascending order
    dataMapList.clear(); // empties the QMap

    i=values.begin();
    j=keys.begin();

    // insert back the sorted values and map them to keys in QMap container
    while(i!=values.end() && j!=keys.end()){

        dataMapList.insert(*j, *i);
        i++;
        j++;
    }


    qDebug() << "sorted" << endl;
    h=dataMapList.begin();
    //the display of the sorted QMap
    while(h!=dataMapList.end()){
        qDebug() << "[" << h.key()<<"], " <<"[" <<h.value()<<"]" << endl;
        h++;
    }



    return a.exec();
}

注意:QMap和QList的迭代器用于遍历容器以访问存储的值和/或键。这些也有助于显示列表中的项目(未排序和已排序)。该解决方案是在Qt控制台应用程序中完成的。请发表评论:-)


3
投票

默认情况下,在QMap中,项目始终按键排序。因此,如果您像这样遍历QMap

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

您将获得按键排序的结果。

尝试考虑将任务转换为适合标准算法的方法。否则,您可以使用这种方法对标题进行排序:

QList<int> list = yourQMap.values();
qSort(list.begin(), list.end());

然后,如果需要-通过调用方法QMap::key(const T &value);获得关联的密钥。


2
投票

另一种选择,根据具体情况而定,只是交换密钥和值,因为密钥将由QMap自动排序。在大多数情况下,值将不是唯一的,因此请改用QMultiMap

例如,假设我们在QMap中具有以下数据:

Key       Value
---       -----
1         100
2         87
3         430
4         87

下面的代码将按值对数据进行排序。

QMap<int, int> oldMap;
QMultiMap<int, int> newMap;
QMapIterator<int, int> it(oldMap);

while (it.hasNext())
{
    it.next();
    newMap.insertMulti(it.value(), it.key()); //swap value and key
}

我们的新地图现在看起来像这样:

Key       Value
---       -----
87        4
87        2
100       1
430       3
© www.soinside.com 2019 - 2024. All rights reserved.