Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. std::transform over QMap

std::transform over QMap

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmapstltransform
13 Posts 5 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Venkateswaran
    wrote on 13 Oct 2020, 10:57 last edited by
    #1

    I have to transform a QMap<QString, int>. I tried to use std::transform as it works for QList, but it's failed to compile.

    #include <QCoreApplication>
        #include <algorithm>
        #include <QMap>
        
        int main(int argc, char *argv[]) {
          QCoreApplication a(argc, argv);
        
          QMap<QString, int> map;
          map["one"] = 1;
          map["three"] = 3;
          map["seven"] = 7;
        
          QMap<QString, int> map2;
        
          std::transform(map.begin(), map.end(), std::back_inserter(map2), [](const QPair<QString, int>& item) {
              return qMakePair(item.first+"testing", item.second);
          });
        
          return a.exec();
        }
    

    I'm getting the below error while compiling the above code. I would like to achieve this without general for loop as my project using cppcheck and it throws warnings.

       /usr/include/c++/7/bits/stl_iterator.h:490: error: no type named ‘value_type’ in ‘class QMap<QString, int>’
        In file included from /usr/include/c++/7/bits/stl_algobase.h:67:0,
                         from /usr/include/c++/7/algorithm:61,
                         from ../../../../Qt5.12.4/5.12.4/gcc_64/include/QtCore/qglobal.h:142,
                         from ../../../../Qt5.12.4/5.12.4/gcc_64/include/QtCore/qcoreapplication.h:43,
                         from ../../../../Qt5.12.4/5.12.4/gcc_64/include/QtCore/QCoreApplication:1,
                         from ../test/main.cpp:1:
        /usr/include/c++/7/bits/stl_iterator.h: In instantiation of ‘class std::back_insert_iterator<QMap<QString, int> >’:
        ../test/main.cpp:15:65:   required from here
        /usr/include/c++/7/bits/stl_iterator.h:490:7: error: no type named ‘value_type’ in ‘class QMap<QString, int>’
               operator=(const typename _Container::value_type& __value)
               ^~~~~~~~
        
        /usr/include/c++/7/bits/stl_algo.h:4306: error: no match for call to ‘(main(int, char**)::<lambda(const QPair<QString, int>&)>) (int&)’
        In file included from /usr/include/c++/7/algorithm:62:0,
                         from ../../../../Qt5.12.4/5.12.4/gcc_64/include/QtCore/qglobal.h:142,
                         from ../../../../Qt5.12.4/5.12.4/gcc_64/include/QtCore/qcoreapplication.h:43,
                         from ../../../../Qt5.12.4/5.12.4/gcc_64/include/QtCore/QCoreApplication:1,
                         from ../test/main.cpp:1:
        /usr/include/c++/7/bits/stl_algo.h: In instantiation of ‘_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = QMap<QString, int>::iterator; _OIter = std::back_insert_iterator<QMap<QString, int> >; _UnaryOperation = main(int, char**)::<lambda(const QPair<QString, int>&)>]’:
        ../test/main.cpp:17:4:   required from here
        /usr/include/c++/7/bits/stl_algo.h:4306:24: error: no match for call to ‘(main(int, char**)::<lambda(const QPair<QString, int>&)>) (int&)’
          *__result = __unary_op(*__first);
                      ~~~~~~~~~~^~~~~~~~~~
        /usr/include/c++/7/bits/stl_algo.h:4306:24: note: candidate: QPair<QString, int> (*)(const QPair<QString, int>&) <conversion>
        /usr/include/c++/7/bits/stl_algo.h:4306:24: note:   candidate expects 2 arguments, 2 provided
        ../test/main.cpp:15:102: note: candidate: main(int, char**)::<lambda(const QPair<QString, int>&)>
           std::transform(map.begin(), map.end(), std::back_inserter(map2), [](const QPair<QString, int>& item) {
                                                                                                              ^
        ../test/main.cpp:15:102: note:   no known conversion for argument 1 from ‘int’ to ‘const QPair<QString, int>&’
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 13 Oct 2020, 11:02 last edited by
      #2

      @Venkateswaran said in std::transform over QMap:

      std::back_inserter(map2)

      How should this work? QMap has no push_back() and can't have it since it needs a key and a value.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      V 1 Reply Last reply 13 Oct 2020, 11:08
      2
      • C Christian Ehrlicher
        13 Oct 2020, 11:02

        @Venkateswaran said in std::transform over QMap:

        std::back_inserter(map2)

        How should this work? QMap has no push_back() and can't have it since it needs a key and a value.

        V Offline
        V Offline
        Venkateswaran
        wrote on 13 Oct 2020, 11:08 last edited by
        #3

        @Christian-Ehrlicher is there any other way to achieve this without general for loop?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 13 Oct 2020, 11:15 last edited by
          #4

          No, but why?

          for (auto it = map.begin(); it != map.end(); ++i)
            map2.insert(it.key + "testing", it.value);
          

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          V 1 Reply Last reply 13 Oct 2020, 11:21
          2
          • C Christian Ehrlicher
            13 Oct 2020, 11:15

            No, but why?

            for (auto it = map.begin(); it != map.end(); ++i)
              map2.insert(it.key + "testing", it.value);
            
            V Offline
            V Offline
            Venkateswaran
            wrote on 13 Oct 2020, 11:21 last edited by
            #5

            @Christian-Ehrlicher Because of cppcheck in my project :) it's not allowing me to use for loop for this.

            J C 2 Replies Last reply 13 Oct 2020, 11:21
            0
            • V Venkateswaran
              13 Oct 2020, 11:21

              @Christian-Ehrlicher Because of cppcheck in my project :) it's not allowing me to use for loop for this.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 13 Oct 2020, 11:21 last edited by
              #6

              @Venkateswaran said in std::transform over QMap:

              it's not allowing me to use for loop for this

              ?
              Can you post what it tells you?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • V Venkateswaran
                13 Oct 2020, 11:21

                @Christian-Ehrlicher Because of cppcheck in my project :) it's not allowing me to use for loop for this.

                C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 13 Oct 2020, 11:28 last edited by
                #7

                @Venkateswaran said in std::transform over QMap:

                it's not allowing me to use for loop for this.

                It's just suggesting to use std functions when possible. But it is not. Blame cppcheck.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • Q Offline
                  Q Offline
                  qwertzui11
                  wrote on 13 Oct 2020, 11:43 last edited by qwertzui11
                  #8

                  I guess the correct code could would look like

                  void fun() {
                    QMap<QString, int> map;
                    QMap<QString, int> map2;
                    std::transform(map.begin(), map.end(), std::inserter(map2, map2.end()),
                                   [](auto &todo) { return todo; });
                  }
                  

                  however there seems to be no QMap<QString, int>::value_type defined. Qts container seem do not stick to the standard, so the std::inserter paradigma does not work.

                  using the standard map, it compiles.

                  void works() {
                    std::map<QString, int> map;
                    std::map<QString, int> map2;
                    std::transform(map.begin(), map.end(), std::inserter(map2, map2.end()),
                                   [](auto &todo) { return todo; });
                  }
                  

                  Have fun
                  Markus

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 13 Oct 2020, 11:44 last edited by Christian Ehrlicher
                    #9

                    @qwertzui11 said in std::transform over QMap:

                    Qts container seem do not stick to the standard,

                    correct because QMap was there earlier :)

                    A range-based for-loop for QMap similar to std::map can be done with https://doc.qt.io/qt-5/qmap-key-iterator.html afair

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    Q 1 Reply Last reply 13 Oct 2020, 12:02
                    3
                    • V Offline
                      V Offline
                      Venkateswaran
                      wrote on 13 Oct 2020, 12:00 last edited by
                      #10

                      Ok. My actual conversion is from QMap<quint16, QPair<QString, quint16>> to QMap<quint16, QPair<QUuid, quint16>> and QHash<QString, int> to QHash<QUuid, int>

                      QMap<quint16, QPair<QUuid, quint16>> map2;
                      for(auto it = map.begin(); it != map.end(); ++it)
                          map2.insert(it.key(), qMakePair(QUuid(it.value().first), it.value().second));
                      
                      QHash<QUuid, int>map2;
                      for(auto it = map.begin(); it != map.end(); ++it)
                          map2.insert(QUuid(it.key()), it.value());
                      

                      and the cppCheck not complaining. Looks like cppcheck complain only if didn't do much inside the loop (I'm not sure).

                      I get the below warning when I convert QList<enum> to QList<QString>.

                      Controller.cpp:120:21: warning: Consider using std::transform algorithm instead of a raw loop. [useStlAlgorithm]
                            versionsInStr.push_back(convertApiVersionToString(ver));
                      

                      Thank you @Christian-Ehrlicher

                      1 Reply Last reply
                      0
                      • C Christian Ehrlicher
                        13 Oct 2020, 11:44

                        @qwertzui11 said in std::transform over QMap:

                        Qts container seem do not stick to the standard,

                        correct because QMap was there earlier :)

                        A range-based for-loop for QMap similar to std::map can be done with https://doc.qt.io/qt-5/qmap-key-iterator.html afair

                        Q Offline
                        Q Offline
                        qwertzui11
                        wrote on 13 Oct 2020, 12:02 last edited by
                        #11

                        @Christian-Ehrlicher agreed. I hope Qt6 fixes these smalls gaps :-)

                        C 1 Reply Last reply 13 Oct 2020, 12:06
                        0
                        • Q qwertzui11
                          13 Oct 2020, 12:02

                          @Christian-Ehrlicher agreed. I hope Qt6 fixes these smalls gaps :-)

                          C Offline
                          C Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 13 Oct 2020, 12:06 last edited by
                          #12

                          @qwertzui11 It can't since it will break a lot of existing stuff - you won't explain all customers that the range-based for loop for containers now returns an iterator instead the value on a QMap and that they have to adjust all of it's code. :(

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 13 Oct 2020, 12:16 last edited by
                            #13

                            Hi,

                            You can use QMap:: keyValueBegin and QMap::keyValueEnd for your use case.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1

                            8/13

                            13 Oct 2020, 11:43

                            • Login

                            • Login or register to search.
                            8 out of 13
                            • First post
                              8/13
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved