In the Qhash class what does the function "QHash::values("key");" return when the key itself is not found in the Qhash object?
-
[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 . -
[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 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 :-)
-
[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.
-
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. -
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.
-
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().