Details
-
Suggestion
-
Resolution: Unresolved
-
Not Evaluated
-
None
-
None
-
None
Description
Hi,
I am using this code :
QMap<T, U> map1; QMap<T, U> map2; if (map1.keys() == map2.keys()) { // ... }
It works perfectly, but when I analyze it with clazy, I get a "container-anti-pattern" warning, because "keys()" allocates and fills a new container, which is not needed to do this kind of comparison.
I talked about it with the clazy maintainer, who told me I could use :
bool keysEqual(const QMap<T, U> &map1, const QMap<T, U> &map2) { if (map1.size() != map2.size()) return false; for (auto it1 = map1.keyBegin(), it2 = map2.keyBegin(), e1 = map1.keyEnd(); it1 != e1; ++it1, ++it2) { if (*it1 != *it2) return false; } return true; }
Which is a couple orders of magnitude faster.
So, I think it would be interesting to add this kind of method in QMap itself (and maybe other containers as well)