Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

std::transform over QMap



  • 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>&’
    

  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    No, but why?

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


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


  • Lifetime Qt Champion

    @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?


  • Lifetime Qt Champion

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



  • 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


  • Lifetime Qt Champion

    @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



  • 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



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


  • Lifetime Qt Champion

    @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. :(


  • Lifetime Qt Champion

    Hi,

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


Log in to reply