Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Learning
  3. Qt in Education
  4. QMap: Iterator and foreach
Forum Updated to NodeBB v4.3 + New Features

QMap: Iterator and foreach

Scheduled Pinned Locked Moved Solved Qt in Education
6 Posts 4 Posters 10.7k Views 2 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.
  • ALLINXXXA Offline
    ALLINXXXA Offline
    ALLINXXX
    wrote on last edited by kshegunov
    #1

    Hi.
    Here's my code;

    #include <QCoreApplication>
    #include <Qdebug>
    #include <QMap>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QMap<int,QString> Employee;
    
        Employee.insert(1,"Bob");
        Employee.insert(2,"Chad");
        Employee.insert(3,"Marry");
        Employee.insertMulti(1,"Amy");
    
        foreach(int i, Employee.keys())
        {
            qDebug() << Employee[i];
        }
    
        QMapIterator<int,QString> Iter(Employee);
        while(Iter.hasNext())
        {
            Iter.next();
            qDebug() << Iter.key() << Iter.value();
        }
    
        return a.exec();
    }
    

    But the result is :
    "Amy"
    "Amy"
    "Chad"
    "Marry"

    1 "Amy"
    1 "Bob"
    2 "Chad"
    3 "Marry"

    So my question is:
    Why the results of "foreach" and "Iterator" is differet????
    I mean,why foreach makes two "Amy"??

    Thanks for helping !

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by Paul Colby
      #2

      Hi @ALLINXXX, welcome :)

      Employee.insert(1,"Bob");
      Employee.insertMulti(1,"Amy")
      

      Here you have two items under the key 1, so Employee.keys() returns 1 twice as per the docs:

      Keys that occur multiple times in the map (because items were inserted with insertMulti(), or unite() was used) also occur multiple times in the list.

      So then this line executes twice with i set to 1.

      qDebug() << Employee[i];
      

      Both times the [] operator just returns the last value associated with the key 1, as per the docs:

      If the map contains multiple items with key key, this function returns a reference to the most recently inserted value.

      Cheers.

      ALLINXXXA 1 Reply Last reply
      4
      • Paul ColbyP Paul Colby

        Hi @ALLINXXX, welcome :)

        Employee.insert(1,"Bob");
        Employee.insertMulti(1,"Amy")
        

        Here you have two items under the key 1, so Employee.keys() returns 1 twice as per the docs:

        Keys that occur multiple times in the map (because items were inserted with insertMulti(), or unite() was used) also occur multiple times in the list.

        So then this line executes twice with i set to 1.

        qDebug() << Employee[i];
        

        Both times the [] operator just returns the last value associated with the key 1, as per the docs:

        If the map contains multiple items with key key, this function returns a reference to the most recently inserted value.

        Cheers.

        ALLINXXXA Offline
        ALLINXXXA Offline
        ALLINXXX
        wrote on last edited by
        #3

        @Paul-Colby
        Appreciate!
        One one Question ,How to turn "UNSOLVED" question to solved question
        thanks! (^o^)/

        kshegunovK 1 Reply Last reply
        0
        • ALLINXXXA ALLINXXX

          @Paul-Colby
          Appreciate!
          One one Question ,How to turn "UNSOLVED" question to solved question
          thanks! (^o^)/

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @ALLINXXX
          Take a look here: http://forum.qt.io/topic/62700/hitchhiker-s-visual-guide-to-the-qt-forum

          Read and abide by the Qt Code of Conduct

          ALLINXXXA 1 Reply Last reply
          1
          • kshegunovK kshegunov

            @ALLINXXX
            Take a look here: http://forum.qt.io/topic/62700/hitchhiker-s-visual-guide-to-the-qt-forum

            ALLINXXXA Offline
            ALLINXXXA Offline
            ALLINXXX
            wrote on last edited by
            #5

            @kshegunov
            Thanks!
            The forum is nice for rookies!

            1 Reply Last reply
            1
            • E Offline
              E Offline
              elishajoseph
              Banned
              wrote on last edited by
              #6
              This post is deleted!
              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