Please nominate your Qt Champions for 2021!

[Solved] QList<T>::iterator vs QListIterator<T>

  • Hi all,
    I've tried to use these two types of iterators and both are working.
    but, what is the difference? or which is better to use?
    thanks in advance

    @ QList<T*> myList;

    QList<T*>::iterator it1= myList.begin();
    for( ; it1 != myList.end(); ++it1)

    QListIterator<T*> it2(myList);

  • Moderators

    There is no "better", those are just two different styles. The first one is the usual C++ style. It's compatible with std algorithms that expect begin and end members. It also gives you easier access to the current index (it - begin).

    The other one is more of a Java style, as that's how they usually go about iteration there. It's a little less verbose I guess (no stars etc.) at a slight price in performance (according to docs).

    But mostly, if you don't need any of the specific properties of one or another, it's a simple style choice.

  • Lifetime Qt Champion


    IIRC it's STL vs Java style iterator. Java style being easier to use but slightly less efficient than STL style.

    You can also your the foreach construct depending on your needs.

    Hope it helps

  • Clear !

Log in to reply