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. How to iterate over a QHash using for_each
Forum Updated to NodeBB v4.3 + New Features

How to iterate over a QHash using for_each

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 202 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.
  • B Offline
    B Offline
    Bondrusiek
    wrote 29 days ago last edited by
    #1

    Hi!
    I'm trying to iterate over a QHash using std::for_each, but I can't accomplish this task. The code I'm using:

    #include <QCoreApplication>
    #include <QHash>
    #include <algorithm>
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QHash<int, int> hash = {{1, 100}, {2, 200}, {3, 300}};
    
        std::for_each(hash.begin(), hash.end(),
                      [](const std::pair<const int, int>& pair) {
                          std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
                      });
    
        return a.exec();
    }
    
    

    but I got error. Any ideas how to solve that ?

    J 1 Reply Last reply 29 days ago
    0
    • B Bondrusiek
      29 days ago

      Hi!
      I'm trying to iterate over a QHash using std::for_each, but I can't accomplish this task. The code I'm using:

      #include <QCoreApplication>
      #include <QHash>
      #include <algorithm>
      #include <iostream>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          QHash<int, int> hash = {{1, 100}, {2, 200}, {3, 300}};
      
          std::for_each(hash.begin(), hash.end(),
                        [](const std::pair<const int, int>& pair) {
                            std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
                        });
      
          return a.exec();
      }
      
      

      but I got error. Any ideas how to solve that ?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote 29 days ago last edited by jsulm
      #2

      @Bondrusiek said in How to iterate over a QHash using for_each:

      [](const std::pair<const int, int>& pair)

      Change to:

      [](const int &key)
      

      See https://en.cppreference.com/w/cpp/algorithm/for_each
      "Applies the given unary function object f"

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote 29 days ago last edited by
        #3

        And what is the use case here? Why not use a simple for loop?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • B Offline
          B Offline
          Bondrusiek
          wrote 29 days ago last edited by
          #4

          I have a task about that. Is it possible use for ranged loop(I also tried implement that but with error)?

          J S 2 Replies Last reply 29 days ago
          0
          • B Bondrusiek
            29 days ago

            I have a task about that. Is it possible use for ranged loop(I also tried implement that but with error)?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote 29 days ago last edited by
            #5

            @Bondrusiek said in How to iterate over a QHash using for_each:

            use for ranged loop

            What do you mean?

            If you tried something and got errors, then please post the code and the error.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bondrusiek
              wrote 29 days ago last edited by
              #6

              This is a loop analogous to the one in the main thread:

                  for (const auto& element : hash)
                  {
                      std::cout << "Key: " << element.key() << " Value: " << element.value() << std::endl;
                  }
              

              but I suppose it's similar to the for_each loop.

              J 1 Reply Last reply 29 days ago
              0
              • B Bondrusiek
                29 days ago

                This is a loop analogous to the one in the main thread:

                    for (const auto& element : hash)
                    {
                        std::cout << "Key: " << element.key() << " Value: " << element.value() << std::endl;
                    }
                

                but I suppose it's similar to the for_each loop.

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote 29 days ago last edited by
                #7

                @Bondrusiek Element is again the key here

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • B Bondrusiek has marked this topic as solved 29 days ago
                • B Bondrusiek
                  29 days ago

                  I have a task about that. Is it possible use for ranged loop(I also tried implement that but with error)?

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote 28 days ago last edited by
                  #8

                  @Bondrusiek if you want to have access to the key and value, you need to use keyValueBegin and keyValueEnd.

                  QHash doesn't match the stl API but it was too late to change when it was discovered.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1

                  3/8

                  15 May 2025, 07:49

                  topic:navigator.unread, 5
                  • Login

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