Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-62142

Add a method to compare the keys of two QMaps

XMLWordPrintable

    • Icon: Suggestion Suggestion
    • Resolution: Unresolved
    • Icon: Not Evaluated Not Evaluated
    • None
    • None
    • None

      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)

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            thiago Thiago Macieira
            kniebou Nicolas Kniebihler
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes