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] QList<T>::iterator vs QListIterator<T>
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.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.
  • B Offline
    B Offline
    BorisSclauzero
    wrote on last edited by
    #1

    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
    boris

    @ QList<T*> myList;

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

    QListIterator<T*> it2(myList);
    while(it2.hasNext())
    it2.next()->doSomething();@

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        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

        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
        0
        • B Offline
          B Offline
          BorisSclauzero
          wrote on last edited by
          #4

          Clear !
          thanks

          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