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.
  • 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

                          11/13

                          13 Oct 2020, 12:02

                          • Login

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