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. Reverse iterate on QMultiMap
Qt 6.11 is out! See what's new in the release blog

Reverse iterate on QMultiMap

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.9k Views
  • 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.
  • J Offline
    J Offline
    Josz
    wrote on last edited by Josz
    #1

    Hello, maybe its a very treated theme, but in moment I coud not find a satisfactory answer in google.

    I have the next QMultiMap List:

        QMultiMap<QString, QString> data;
        data.insert("John", "Young");
        data.insert("John", "Tall");
        data.insert("Mike", "Short");
        data.insert("Mike", "Good man");
        data.insert("John", "Clueless");
        data.insert("Mike", "Long hair");
    
        for(const QString &name : data.uniqueKeys()){
            QStringList aux = data.values(nombre);
            qDebug() << "Name: " << name;
    
            for(const QString &aux2 :  aux) {
                qDebug() << aux2;
            }
        }
    

    The code produces the next output

    Name:  "John"
    "Clueless"
    "Tall"
    "Young"
    Name:  "Mike"
    "Long hair"
    "Good man"
    "Short"
    

    The list is in The list is in random order displayed.

    Is there a way to show the elements in reverse order? .... Or in a determined order, for example, alphabetical?

    Thanks in avance.

    JonBJ 1 Reply Last reply
    0
    • J Josz

      Hello, maybe its a very treated theme, but in moment I coud not find a satisfactory answer in google.

      I have the next QMultiMap List:

          QMultiMap<QString, QString> data;
          data.insert("John", "Young");
          data.insert("John", "Tall");
          data.insert("Mike", "Short");
          data.insert("Mike", "Good man");
          data.insert("John", "Clueless");
          data.insert("Mike", "Long hair");
      
          for(const QString &name : data.uniqueKeys()){
              QStringList aux = data.values(nombre);
              qDebug() << "Name: " << name;
      
              for(const QString &aux2 :  aux) {
                  qDebug() << aux2;
              }
          }
      

      The code produces the next output

      Name:  "John"
      "Clueless"
      "Tall"
      "Young"
      Name:  "Mike"
      "Long hair"
      "Good man"
      "Short"
      

      The list is in The list is in random order displayed.

      Is there a way to show the elements in reverse order? .... Or in a determined order, for example, alphabetical?

      Thanks in avance.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Josz
      I assume you mean the "multi-items" associated with each key in reverse order. Then don't use for(const QString &aux2 : aux) : use a reverse iterator, or count down from aux.length() - 1 yourself, or reverse the QStringList.

      J 1 Reply Last reply
      4
      • JonBJ JonB

        @Josz
        I assume you mean the "multi-items" associated with each key in reverse order. Then don't use for(const QString &aux2 : aux) : use a reverse iterator, or count down from aux.length() - 1 yourself, or reverse the QStringList.

        J Offline
        J Offline
        Josz
        wrote on last edited by
        #3

        @JonB
        thank you john! I will take a look

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          QMap iterators are bidirectional: for(auto i = map.keyEnd()-1;i!=map.keyBegin();--i)

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          4

          • Login

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