首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >std::QMap的转换

std::QMap的转换
EN

Stack Overflow用户
提问于 2020-10-13 10:00:55
回答 1查看 669关注 0票数 2

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

代码语言:javascript
复制
#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并抛出警告。

代码语言:javascript
复制
/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>&’
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-13 10:45:44

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

您还存在一个问题,即标准插入程序希望在容器中定义一个value_type,而QMap没有这个问题。在旧版本中,QMap::value_type被用于与std::map::value_type不同的含义。

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

代码语言:javascript
复制
#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

代码语言:javascript
复制
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();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64332990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档