c++stlqmap

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

Solution

  • A major problem is std::back_inserter(map2) wants to call push_back, which is not a member of QMap (nor std::map).

    You also have the issue that the standard inserters want a value_type defined in the container, that QMap doesn't have. In older versions, QMap::value_type was used for a different meaning to std::map::value_type.

    You could define an insert iterator yourself

    #include <QCoreApplication>
    #include <algorithm>
    #include <QMap>
    
    template <typename K, typename T>
    struct QMapInsertIterator {
        QMap<K, T> & map;
        QMapInsertIterator& operator=(const QPair<K, T> & pair) { map.insert(pair.first, pair.second); return *this; }
        QMapInsertIterator& operator++() { return *this; }
        QMapInsertIterator& operator*() { return *this; }
    };
    
    template <typename K, typename T>
    QMapInsertIterator<K, T> QMapInserter(QMap<K, T> & map) {
        return { map };
    }
    

    Which directly replaces std::back_inserter

    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(), QMapInserter(map2), [](const QPair<QString, int>& item) {
          return qMakePair(item.first+"testing", item.second);
      });
    
      return a.exec();
    }