c++ - 在 QMap 上的 std::transform
问题描述
我必须转换一个 QMap<QString, int>。我尝试使用 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
(nor std::map
) 的成员。
您还存在标准插入器想要value_type
在容器中定义的问题,而QMap
没有。在旧版本中,QMap::value_type
用于与std::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();
}
推荐阅读
- python - RuntimeError: 3 维权重 [128, 1024, 1] 的预期 3 维输入,但得到了大小为 [32, 1024] 的 2 维输入
- azure-devops - Azure DevOps 服务器 - 多个应用程序层
- plugins - 我的 QuickBooks Online 应用程序是否需要在单独的窗口或选项卡中运行?是否有像插件一样运行的应用程序,在与 QB Online 相同的窗口中?
- html - 为什么会出现水平滚动条?
- r - 将多列合并为一个列表列
- python - 如何使用 pandas 从 CSV 打印出特定列
- spring-boot - 使用多个数据源的 LazyInitializationException
- java - ref 的字符串赋值。将对象字符串到内存中会发生在此语句之后吗?
- php - 无法在某些子进程成为僵尸之前终止它们
- android - 如何为 Jenkins 配置 docker 镜像以创建 Android 构建?