[SOLVED] List operations



  • 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?



  • 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.



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



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



  • 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@



  • Great, did it with:

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

    Thx.



  • [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.



  • 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.



  • 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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.