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] List operations
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] List operations

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 15.3k 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.
  • D Offline
    D Offline
    david.luggen
    wrote on last edited by
    #1

    Hello

    Because I'm quite new to -QT- Qt I have this a bit more general Question. Let's assume I have a text file filled with float numbers. These numbers are fielded by "," and listed in lines (number of lines is variable between 0 - 10'000). There is no problem to load them by single fields in different -QT- Qt Objects. Secondly, I have to pass them between several classes and I need the possibility to do statistic calculations with the lists, e.g. get the max and min value.

    Now, which one should I take?

    QList<float>, QVector or should I create just an array?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      That would depend on what operations you need to do on the list itself, among other criteria. If you are going to read the list once, and then don't need to modify it anymore, you can consider using QVector<>. If you need to modify the list, especially if you need to insert or remove items in the middle of it, QList is a much better choice.

      Both containers are implicitly shared, so passing them as values between classes is a very cheap operation. On the downside: modifying the list is a bit more expensive.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        david.luggen
        wrote on last edited by
        #3

        Okay, thx. How can I now get the max value of a QList<float>? Seems pretty involved.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Iterate over the whole list, obviously. Or, by keeping the list sorted, and taking the first (or the last) item.

          1 Reply Last reply
          0
          • ZlatomirZ Offline
            ZlatomirZ Offline
            Zlatomir
            wrote on last edited by
            #5

            About QList or QVector - those are two very related data structures (QList is not a linked list - Qt has QLinkedList for that), read the documentation to see the differences between QList and QVector.

            I'm not sure, but as far as i know Qt has no function that returns the max value (but it shouldn't be that difficult to code one).

            But i guess you can use the STL functionality, something like:
            @QList<float>::iterator it = std::max_element(l.begin(), l.end()); //i didn't test it@

            https://forum.qt.io/category/41/romanian

            1 Reply Last reply
            0
            • D Offline
              D Offline
              david.luggen
              wrote on last edited by
              #6

              Great, did it with:

              @qSort(list.begin(), list.end(), qGreater<float>());
              float maxValue = list[0];@

              Thx.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                [quote author="luggi" date="1306164458"]Great, did it with:

                @qSort(list.begin(), list.end(), qGreater<float>());
                float maxValue = list[0];@

                Thx.[/quote]
                That is an expensive solution... Unless you already need your list sorted, I would not sort it for just this issue. Just iterating over the list, using std as Zlatomir suggests if you want, would be much faster.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  david.luggen
                  wrote on last edited by
                  #8

                  Hmm, I didn't really understand how I can now extract one single float-value out of this iterator?
                  By the way, "max_element" does not exist, but I found "max". Should be the same thing.

                  1 Reply Last reply
                  0
                  • ZlatomirZ Offline
                    ZlatomirZ Offline
                    Zlatomir
                    wrote on last edited by
                    #9

                    It exist but you need to include the algorithm header (i said it's from the STL) and an iterator is a generalization of a pointer, so to get the value you need to dereference the iterator.

                    I tested the code and it works, here is a complete example:
                    @#include <iostream>
                    #include <algorithm>
                    #include <QList>

                    int main()
                    {
                    QList<float> l;

                    l << 1 << 2.2 << 3.5 << 4.6 << 7 << 13  << 15 << 35.82 << 0 << 12 << 8.34;
                    
                    QList<float>::iterator it = std::max_element(l.begin(), l.end());
                    
                    
                    std::cout << "The bigger element from the list is: " << *it << '\n';
                    std::cin.get();
                    return 0;
                    

                    }@

                    LE: if you don't get the expected results about float comparison read "this":http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.17

                    https://forum.qt.io/category/41/romanian

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      david.luggen
                      wrote on last edited by
                      #10

                      Okay, it was the max which dumped something else, thx!

                      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