[SOLVED] QMap:: How to remove some values from all keys
-
Each key has a value.
How to remove each key value with an arbitrary sequence number@
#include <QtCore/QCoreApplication>
#include <QMap>
#include <QString>
#include <QStringList>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QMap <QString,QStringList> qmapCollection;
QMap <QString,QStringList>::iterator it;
int i =0;qmapCollection["One"].append("1"); qmapCollection["One"].append("11"); qmapCollection["One"].append("111"); qmapCollection["Two"].append("2"); qmapCollection["Two"].append("22"); qmapCollection["Two"].append("222"); qmapCollection["Three"].append("3"); qmapCollection["Three"].append("33"); qmapCollection["Three"].append("333"); return a.exec();
}
@I want to remove each key a second value,
not the value itself, and the ordinal value of the index [2] by all keys:
This is what should be output:One - 1
One - 111
Two - 2
Two - 222
Three-3
Three-333 -
-You can use "remove":http://qt-project.org/doc/qt-4.8/qmultimap.html#remove for that. Just check if the key has changed and if so reset an interal int iterator to 1 if this internal iterator modulo 2 is 0 you erase the entry pointed to by the QMap::iterator.-
-This would remove every second entry for a key, if you just want to remove the second value for a key just check if the int iterator is two instead of the modulo stuff.-
@
QMultiMap<QString, int>::iterator mapIt = yourMap.begin();
QString key = mapIt.key();
int it = 0;
while(mapIt != yourMap.end())
{
if(key != mapIt.key())
{
key = mapIt.key();
it = 1;
if(it % 2 == 0)
{
yourMap.remove(key,mapIt.value());
}
}
mapIt++;
it++;
}
@-Didn't test this. This is just an idea.-
Edit: Just realized my solution was for a "QMultiMap":http://qt-project.org/doc/qt-4.8/qmultimap.html <QString, int> and not for your case QMap<QString, QStringList>
@
QMap<QString, QStringList>::iterator mapIt = yourMap.begin();
while(mapIt != yourMap.end())
{
QString key = mapIt.key();
QStringList oldValue = mapIt.value();
QStringList newValue;
for(int counter = 0; counter < oldValue.size(); counter++)
{
if(counter != 1)
{
newValue << oldValue.at(counter);
}
}
//this overwrites the old value
yourMap.insert(key,newValue);
mapIt++;
}
@This should remove the second entry in the QStringList returned as a keys value.
-
with erase() I tried, the key is removed with all the values.
In the end, get two keys instead of three.I want every second (third, fourth) the value removed from all key
-
Can you please give the code in relation to my case
QMap<QString,QStringlist>
QMultiMap<QString, QStringlist>with
@
qmapCollection["One"].append("1");
qmapCollection["One"].append("11");
qmapCollection["One"].append("111");
qmapCollection["Two"].append("2");
qmapCollection["Two"].append("22");
qmapCollection["Two"].append("222");
qmapCollection["Three"].append("3");
qmapCollection["Three"].append("33");
qmapCollection["Three"].append("333");
@ -
Thank you very much!!!
Solved!
-
Full code:
@
#include <QtCore/QCoreApplication>
#include <QMap>
#include <QString>
#include <QStringList>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QMap <QString,QStringList> qmapCollection;
QMap<QString, QStringList>::iterator mapIt;QString key = NULL; QStringList oldValue ; QStringList newValue ; qmapCollection["One"].append("1"); qmapCollection["One"].append("11"); qmapCollection["One"].append("111"); qmapCollection["Two"].append("2"); qmapCollection["Two"].append("22"); qmapCollection["Two"].append("222"); qmapCollection["Three"].append("3"); qmapCollection["Three"].append("33"); qmapCollection["Three"].append("333"); mapIt = qmapCollection.begin(); while(mapIt != qmapCollection.end()) { key = mapIt.key(); oldValue = mapIt.value(); newValue.clear(); /// <-------------------------------- for(int counter = 0; counter < oldValue.size(); counter++) { if(counter != 1) { newValue << oldValue.at(counter); } } qmapCollection.insert(key,newValue); mapIt++; } return a.exec();
}
@