Created
January 2, 2020 19:33
-
-
Save RoyBellingan/f23d98ee7f73cb16c6a91ac7f14dbd73 to your computer and use it in GitHub Desktop.
use the string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QDebug> | |
#include <QMap> | |
#include <QByteArray> | |
class Something { | |
public: | |
QByteArray blob1; | |
int id2 = 0; | |
}; | |
class Weird1 { | |
public: | |
QByteArray name; | |
uint32_t id = 0; //Only this parameter is used to define the UNIQUENESS!!! | |
Something s1; | |
}; | |
bool operator<(const Weird1& e1, const Weird1& e2) { | |
if (!e1.name.isEmpty() && !e2.name.isEmpty()) { //if both are set | |
//compare the string | |
return e1.name < e2.name; | |
} else { | |
//if not just use the ID | |
return e1.id < e2.id; | |
} | |
} | |
//to print | |
QDebug operator<<(QDebug debug, const Weird1& c) { | |
QDebugStateSaver saver(debug); | |
debug.nospace() << '(' << c.id << ", " << c.name << ')'; | |
return debug; | |
} | |
int main() { | |
QMap<Weird1, QByteArray> qtMap; | |
Weird1 k1, k2; | |
//string is set, so pre-empt id | |
k1.id=10; | |
k1.name = "k1"; | |
k2.id=1; | |
k2.name = "k2"; | |
qtMap.insert(k1, "v1"); | |
qtMap.insert(k2, "v2"); | |
qDebug() << qtMap; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment