Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QMap:: How to remove some values from all keys
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QMap:: How to remove some values from all keys

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 6.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VladimirPivovar
    wrote on last edited by
    #1

    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(&#41;;
    

    }
    @

    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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KA51O
      wrote on last edited by
      #2

      -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.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        VladimirPivovar
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KA51O
          wrote on last edited by
          #4

          Just updated my previous post. Sorry for the missunderstanding. Maybe you can use QMultiMap<QString, int> or QMultiMap<QString, QString> instead of QMap<QString, QStringList>. I think that might be more convenient in your case.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VladimirPivovar
            wrote on last edited by
            #5

            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");
            @

            1 Reply Last reply
            0
            • T Offline
              T Offline
              twsimpson
              wrote on last edited by
              #6

              Instead of
              @QMap<QString, QStringList> qmapCollection;@

              You use
              @QMultiMap<QString, QString> qmapCollection;@

              and insert values with:
              @
              qmapCollection.insert("One", "1");
              qmapCollection.insert("One", "11");
              //etc
              @

              1 Reply Last reply
              0
              • V Offline
                V Offline
                VladimirPivovar
                wrote on last edited by
                #7

                Thank you very much!!!

                Solved!

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  VladimirPivovar
                  wrote on last edited by
                  #8

                  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&#40;&#41;;
                  

                  }

                  @

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved