#include <QCoreApplication>
#include <QDebug>
#include <QMap>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
struct CustomKey{
int a;
int b;
QString c;
bool operator <(const CustomKey & o)const // 必须重载这个运算符
{
if(this->a < o.a)
return true;
else
return false;
}
};
QMap<CustomKey,QList<int>> testmap;
QList<int> list;
list<<1<<2;
CustomKey cKey;
cKey.a = 1;
cKey.b = 2;
cKey.c = QString("test");
testmap.insert(cKey,list);
return a.exec();
}
如果不重载运算符会报如下错误:
C:\Qt\Qt5.12.9\5.12.9\mingw73_64\include\QtCore\qmap.h:71: error: no match for 'operator<' (operand types are 'const main(int, char**)::CustomKey' and 'const main(int, char**)::CustomKey')
return key1 < key2;
~~~~~^~~~~~