Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in
read-only mode
.
Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).
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>&’
Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
Visit the Qt Academy at https://academy.qt.io/catalog
Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
Visit the Qt Academy at https://academy.qt.io/catalog
@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?
@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.
Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
Visit the Qt Academy at https://academy.qt.io/catalog
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
@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
Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
Visit the Qt Academy at https://academy.qt.io/catalog
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