QMultimap - Find all the keys in common with another key
-
Hi to all,
I'm using QMultimap to store some values in my application in this way:
struct scheda { QString gruppo_muscolare; QString esercizio; QString serie; QString ripetizioni; }; QMultiMap<QString, scheda> tabella;Then, I populate it by using a for loop after a SQL query:
model.setQuery("..."); for (int i = 0; i < model.rowCount(); ++i) { tabella.insert(model.record(i).value("scheda.tipo").toString(), {model.record(i).value("gruppo_muscolare.nome").toString(), model.record(i).value("esercizio.nome").toString(), model.record(i).value("esercizio_scheda.serie").toString(), model.record(i).value("esercizio_scheda.ripetizioni").toString()}); }and I print all the keys associate to a specific key in this way:
if(tabella.contains("A")){ QMultiMap<QString, scheda>::iterator i = tabella.find("A"); while (i != tabella.end() && i.key() == "A") { qDebug() << "scheda tipo: " << i.key() << "gruppo muscolare: " << i->gruppo_muscolare << " - esercizio: " << i->esercizio << " - serie: " << i->serie << " - rep: " << i->ripetizioni; ++i; }and I get this result:
scheda tipo: "A" gruppo_muscolare: "pettorali" nome: "incline press hammer" serie: "1" ripetizioni: "2"
scheda tipo: "A" gruppo_muscolare: "pettorali" nome: "Chest Press " serie: "3" ripetizioni: "2"
scheda tipo: "A" gruppo_muscolare: "quadricipiti" nome: "Leg Curl" serie: "2" ripetizioni: "3"I would like to group the results when gruppo_muscolare has the same value in order to trigger different actions.
for example:
if gruppo_muscolare == "pettorali"
then do this
else if gruppo_muscolare == "quadricipiti"
then do this
..the problem is that I can't suppose to know what "gruppo_muscolare" will contain each time so I think I need to group all the keys which have in common the same value in "gruppo_muscolare".
How can I do that?
-
you need a map for each data field that you intend to use as a "key". IIUC you are asking for reverse lookup, where the data field becomes the key and the key becomes the data field. There may be a more elegant algorithmic solution, but given your description one is not immediately evident.
Reading between the lines though, if you are aggregating this data from a SQL database then why not let the DB do the grunt work and concentrate on proper SQL queries to pull the information, grouped as you desire?
-
Hi to all,
I'm using QMultimap to store some values in my application in this way:
struct scheda { QString gruppo_muscolare; QString esercizio; QString serie; QString ripetizioni; }; QMultiMap<QString, scheda> tabella;Then, I populate it by using a for loop after a SQL query:
model.setQuery("..."); for (int i = 0; i < model.rowCount(); ++i) { tabella.insert(model.record(i).value("scheda.tipo").toString(), {model.record(i).value("gruppo_muscolare.nome").toString(), model.record(i).value("esercizio.nome").toString(), model.record(i).value("esercizio_scheda.serie").toString(), model.record(i).value("esercizio_scheda.ripetizioni").toString()}); }and I print all the keys associate to a specific key in this way:
if(tabella.contains("A")){ QMultiMap<QString, scheda>::iterator i = tabella.find("A"); while (i != tabella.end() && i.key() == "A") { qDebug() << "scheda tipo: " << i.key() << "gruppo muscolare: " << i->gruppo_muscolare << " - esercizio: " << i->esercizio << " - serie: " << i->serie << " - rep: " << i->ripetizioni; ++i; }and I get this result:
scheda tipo: "A" gruppo_muscolare: "pettorali" nome: "incline press hammer" serie: "1" ripetizioni: "2"
scheda tipo: "A" gruppo_muscolare: "pettorali" nome: "Chest Press " serie: "3" ripetizioni: "2"
scheda tipo: "A" gruppo_muscolare: "quadricipiti" nome: "Leg Curl" serie: "2" ripetizioni: "3"I would like to group the results when gruppo_muscolare has the same value in order to trigger different actions.
for example:
if gruppo_muscolare == "pettorali"
then do this
else if gruppo_muscolare == "quadricipiti"
then do this
..the problem is that I can't suppose to know what "gruppo_muscolare" will contain each time so I think I need to group all the keys which have in common the same value in "gruppo_muscolare".
How can I do that?
@Marcus-Barnet
As @Kent-Dorfman has said.You might choose to move the work to
GROUPstatements at the SQL database side.But if you do stick to your
QMultiMap. You are perhaps already aware, but you can address your requirement via: ISTM that you need a newQMultiMapwhose keys are the values you encounter inscheda.gruppo_muscolare. Populate that from the records you get back from your query. Now you can enumerate this new map's keys (e.g. in alphabetical order) to retrievegruppo_muscolaregrouped.