对QMap中的key进行自定义排序

本文介绍如何通过重载比较运算符来实现QMap中的自定义类型键按特定规则排序,包括升序和降序示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

QMap的默认排序是按照key的升序进行排序。

如果我们想改变QMap的key的排序规则,则需要提供operator<()

QMap’s key type must provide operator<(). QMap uses it to keep its items sorted, and assumes that two keys x and y are equal if neither x < y nor y < x is true.

Qt帮助文档给的例子:

 #ifndef EMPLOYEE_H
  #define EMPLOYEE_H

  class Employee
  {
  public:
      Employee() {}
      Employee(const QString &name, const QDate &dateOfBirth);
      ...

  private:
      QString myName;
      QDate myDateOfBirth;
  };

  inline bool operator<(const Employee &e1, const Employee &e2)
  {
      if (e1.name() != e2.name())
          return e1.name() < e2.name();
      return e1.dateOfBirth() < e2.dateOfBirth();
  }

  #endif // EMPLOYEE_H

也可以把operator<写成类成员函数。

自己写的一个例子:

在Qt中新建一个控制台程序:

main.cpp中

#include <QCoreApplication>
#include <QMap>
#include <QDebug>

class MyString
{
public:
    explicit MyString(const QString &str_) : str(str_) {}

    QString str;
    //重载操作符
    bool operator < (const MyString& other) const
    {
        return str > other.str;
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QMap<MyString,int> stringMap;
    //初始化数据
    //数据插入容器
    stringMap.insert(MyString("aa"), 4);
    stringMap.insert(MyString("bb"), 3);
    stringMap.insert(MyString("cc"), 2);
    stringMap.insert(MyString("dd"), 1);

    QMapIterator<MyString, int> iter(stringMap);
    while (iter.hasNext()) {
        iter.next();
        qDebug() << iter.key().str << ": " << iter.value() << Qt::endl;
    }

    return a.exec();
}

运行结果:
在这里插入图片描述
就变成了key是按照QString进行降序排列。

如果代码改成这样:

class MyString
{
public:
    explicit MyString(const QString &str_) : str(str_) {}

    QString str;
    //重载操作符
    bool operator < (const MyString& other) const
    {
        return str < other.str;  //这里就改成小于号,QMap里的排序就成了升序和QMap<QString, int>默认排序一样了
    }
};

我再写一个按照int降序排序的例子:

#include <QCoreApplication>
#include <QMap>
#include <QDebug>

class MyInt
{
public:
    explicit MyInt(const int &value_) : value(value_) {}

    int value;
    //重载操作符
    bool operator < (const MyInt& other) const
    {
        return value > other.value;
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QMap<MyInt, QString> intMap;
    //初始化数据
    //数据插入容器
    intMap.insert(MyInt(1), QString("aa"));
    intMap.insert(MyInt(2), QString("bb"));
    intMap.insert(MyInt(3), QString("cc"));
    intMap.insert(MyInt(4), QString("dd"));

    QMapIterator<MyInt, QString> iter(intMap);
    while (iter.hasNext()) {
        iter.next();
        qDebug() << iter.key().value << ": " << iter.value();
    }

    return a.exec();
}

运行结果:
在这里插入图片描述
以上只是为了让举例的代码简单明了,没有写成get和set的方式对类成员变量进行操作,在项目中建议写出get和set的方法对类成员变量进行操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值