std::transform 在 QMap 上

std::transform over QMap

我必须转换一个 QMap。我尝试使用 std::transform,因为它适用于 QList,但编译失败。

#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();
}

编译上述代码时出现以下错误。我想在没有一般 for 循环 的情况下实现这一点,因为我的项目使用 cppcheck 并且它会抛出警告。

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

一个主要问题是 std::back_inserter(map2) 想要调用 push_back,它不是 QMap 的成员(也不是 std::map)。

您还有一个问题,即标准插入器需要在容器中定义 value_type,而 QMap 没有。在旧版本中,QMap::value_typestd::map::value_type 的含义不同。

你可以自己定义一个插入迭代器

#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 };
}

直接替换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();
}