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. Iterator invalidation
QtWS25 Last Chance

Iterator invalidation

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.5k 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.
  • D Offline
    D Offline
    drozel
    wrote on last edited by
    #1

    What is invalidation rules of QT Container iterators?
    Here said:

    Iterators of both types are invalidated when the data in the container is modified or detached from implicitly shared copies due to a call to a non-const member function.
    

    I understand this:

    QMap<qint32, QString> myMap;
    myMap[0] = "zero";
    myMap[1] = "one";
    myMap[2] = "two";
    
    QMap<qint32, QString>::iterator it = myMap.begin();
    myMap[3] = "three";
    ++it;   //error, iterator is invalid after container was modified
    

    But here said:

    Multiple iterators can be used on the same map. If you add items to the map, existing iterators will remain valid. If you remove items from the map, iterators that point to the removed items will become dangling iterators.
    
    

    According to that text, my code is valid and i can remove or add elements to my map without iterator invalidation.
    std::map allows change container without iterator invalidation.

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

      Hi and welcome to devnet,

      See the Implicit sharing iterator problem part of the documentation you linked to have a more detailed explanation about what might be going on with Qt's implicitly shared containers.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        See the Implicit sharing iterator problem part of the documentation you linked to have a more detailed explanation about what might be going on with Qt's implicitly shared containers.

        D Offline
        D Offline
        drozel
        wrote on last edited by drozel
        #3

        @SGaist thank you for your answer.
        I have read it. Let realize that i have only one container and don`t copy it. Can i use iterator and change container? Or is it a QT rule: don`t use iterator when container can be changed?

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

          You have several examples in the detailed description QMap::iterator that do exactly that.

          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

          • Login

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