How to add an item to a QList inside a QMap?
-
Hi everyone,
I'm designing a structure in my programa and I have something like this:QMultiMap<QString, QList<QString> > topicList;
Every QString (key) should be a topic in my MQTT protocol and every QList (value) is a list of the Client's ID subscribed to the previous topic.
I've read the documentation buy I can not understand how to add an element to my QList without overwriting anything else. I'd like to be able to search the IDs of the connected clients to a certain topic and add o remove IDs.
Is this possible? What should I do?Many thanks,
Fernando -
From what you describe you want a map, not a multimap:
QMap<QString, QList<QString> > topicList; //add some IDs to a topic: topicList["foo"].append("bar"); topicList["foo"].append("bazz"); //find a list of IDs for a topic: auto it = topicList.find("foo"); if (it != topicList.end()) { //remove an ID from that topic: it->removeOne("bar"); }
-
Thanks for the reply! I realized that I don't need a MultiMap and I used a Map instead. I had an error when I used "auto" then I had to change a few things. Here is my code:
if(topicList.contains("foo")){ if(!topicList["foo"].contains("bar")){ topicList["bar"].append("bar"); } }else{ QList<QString> aux; aux.append("bar"); topicList.insert("foo",aux); }
The first part checks if the topic "foo" exists. If it's true, then checks if the ID is already registered. I think that is faster to search only in the QList. Besides, the same ID could be subscribed to many topics.
If there is no topic "foo", I have to add an element to my QMap<QString, QList<QString> >. It works (or at least I got no errors) but I'm not sure if there is another way to add my QList, a better way I mean.
Again, many thanks for your reply, it helps me a lot :)
Best regards,
Fernando -
I had an error when I used "auto"
auto
is a c++11 keyword. If it's not enabled by default with your compiler you can addCONFIG += c++11
to your .pro file. If you don't want to useauto
that's ok. Just more typing.I think that is faster
This code is anything but fast.
operator[]
andcontains()
are search algorithms, quite heavy. Also remember that ifoperator[]
doesn't find an item it creates it.
Consider this code:if(topicList.contains("foo")){ if(!topicList["foo"].contains("bar")){ topicList["bar"].append("bar"); } }
It does 4 searches and 1 insertion in the worst case. Now if written like this
QMap<QString, QList<QString> >::iterator it = topicList.find("foo"); //or you could use auto if you enable c++11 if (it != topicList.end()) { QList<QString>::iterator it2 = it->find("bar"); if (it2 != it->end()) it2->append("bar"); }
this is more code but does only 2 searches and 1 insertion in the worst case. This can be simplified like this:
QList<QString>& foo = topicList["foo"]; //this will find a list or create an empty one if it wasn't there QList<QString>::iterator it = foo.find("bar"); if (it != foo.end()) foo.append("bar");
As for the other part, the code is wrong. It will always overwrite entire list and you just want to add to an existing one or create one if it wasn't there, so instread of:
QList<QString> aux; aux.append("bar"); topicList.insert("foo",aux);
it should be:
topicList["foo"].append("bar");
-
I'm such a newbie programmer :(
I study electronics engineer and I like programming but I don't receive a good foundation in my university and I had to learn a lots of things by myself. Also, I haven't done any OOP for years and I'm new in Qt.
Many many thanks!! I will pay more attention to my coding :)