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

How to iterate over a QHash using for_each

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 79 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.
  • BondrusiekB Offline
    BondrusiekB Offline
    Bondrusiek
    wrote 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 ?

    jsulmJ 1 Reply Last reply
    0
    • BondrusiekB Bondrusiek

      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 ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote 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
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote 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
        • BondrusiekB Offline
          BondrusiekB Offline
          Bondrusiek
          wrote 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)?

          jsulmJ SGaistS 2 Replies Last reply
          0
          • BondrusiekB Bondrusiek

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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote 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
            • BondrusiekB Offline
              BondrusiekB Offline
              Bondrusiek
              wrote 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.

              jsulmJ 1 Reply Last reply
              0
              • BondrusiekB Bondrusiek

                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.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote 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
                • BondrusiekB Bondrusiek has marked this topic as solved
                • BondrusiekB Bondrusiek

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

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote 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

                  • Login

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