In the Qhash class what does the function "QHash::values("key");" return when the key itself is not found in the Qhash object?
-
wrote on 6 Dec 2011, 10:20 last edited by
I have tried to check it but not came into any conclusion.
-
wrote on 6 Dec 2011, 10:52 last edited by
It will return an empty QList<T>.
-
wrote on 6 Dec 2011, 11:22 last edited by
[quote author="Andre" date="1323168723"]It will return an empty QList<T>.[/quote]
So how can i check that it is returning empty QList<T> by code? I have to check this before doing some operation to avoid failure.
-
wrote on 6 Dec 2011, 11:49 last edited by
@
if(yourHash.contains("key")) {
// do something
} else {
qDebug() << "key not in the hash";
}
@You did read the [[Doc:QHash]] API docs and just overlooked that method, didn't you?
-
wrote on 6 Dec 2011, 11:55 last edited by
[quote author="Volker" date="1323172165"]@
if(yourHash.contains("key")) {
// do something
} else {
qDebug() << "key not in the hash";
}
@You did read the [[Doc:QHash]] API docs and just overlooked that method, didn't you?[/quote]
Ya i have read that but just missed . -
wrote on 6 Dec 2011, 13:01 last edited by
Or, you just test the returned QList:
@
QList<T> results = myHash.values("key");
if (results.isEmpty()) {
qDebug() << "key not in the hash";
} else {
//use the results to do something useful
}
@ -
wrote on 6 Dec 2011, 13:33 last edited by
[quote author="Andre" date="1323176466"]Or, you just test the returned QList:
@
QList<T> results = myHash.values("key");
if (results.isEmpty()) {
qDebug() << "key not in the hash";
} else {
//use the results to do something useful
}
@
[/quote]Better to check if the key is in the hash, because the hash could had been initialized with a key and empty list.
-
wrote on 6 Dec 2011, 13:40 last edited by
[quote author="fluca1978" date="1323178404"]
Better to check if the key is in the hash, because the hash could had been initialized with a key and empty list.
[/quote]If T is QList<T2>, then method values() returns a list of lists. In the case you described, it would return a QList< QList<T> > containing that empty list :-)
-
wrote on 6 Dec 2011, 14:05 last edited by
[quote author="fluca1978" date="1323178404"]
[quote author="Andre" date="1323176466"]Or, you just test the returned QList:
@
QList<T> results = myHash.values("key");
if (results.isEmpty()) {
qDebug() << "key not in the hash";
} else {
//use the results to do something useful
}
@
[/quote]Better to check if the key is in the hash, because the hash could had been initialized with a key and empty list.
[/quote]You have a point if you talk about QHash::value() (singular version). In that case, you will get a default-constructed T back, which might be indistinguishable from the value you put in the hash in the first place. However, for QHash::values() (plural version), the difference really is there. An empty list is easy to distinguish from a list with an empty or default-constructed element.
-
wrote on 6 Dec 2011, 15:52 last edited by
It doesn't matter, Andre. If anything at all is inside the QHash, then the value will be there and the returned QList will have size() 1, it doesn't matter if it is a default value or a value you inserted or if the item is a QList or not.
As for the code, efficiency-wise, Volker has the best solution, as it avoids the unnecessary creation of a QList. -
wrote on 6 Dec 2011, 16:25 last edited by
veeeee_d:
I tested this code:
@
QHash<QString, QString> testHash;
testHash.insert("foo", "bar");
qDebug() << "output size" << testHash.values("testKey").count();
@
Output is:
@
output size 0
@
Meaning that indeed an empty QList will be returned if there is no item with the specified key in the hash.You are right that that object will need to be created, and that takes time. I guess it depends on your use case what the better way is. If in 99% of the cases you expect the key to be in the hash, then I think my version is faster. If you expect that in the majority of cases the key will not be in the hash, I guess checking first will be faster. You'd have to measure the performance to be sure, I think. Remember that the actual lookup of the value isn't free either. For one, it depends on the type of key used how expensive the actual hash function is.
-
wrote on 6 Dec 2011, 16:44 last edited by
Actually, no. In the specific case you need to find and access the key you are looking for in the QHash, you should use function find(), which returns an iterator to the item, which can then access the item with no additional searching cost.
Refer to "this":http://doc.qt.nokia.com/4.7/qhash.html#find function, and you will see the best solution to your suggested problem. The cost of allocating the QList's buffer alone is worse than the whole cost of iterating through find(). -
wrote on 7 Dec 2011, 07:35 last edited by
That is a good suggestion, and is probably the fastest code path. I did not think of using find() in such a scenario.
4/13